???菜鸟刚学用c#做asp.net网页,我想实现以下功能。我点了一个大类,比如农业,在新打开的网页上去数据库的表里查询属于农业的小类来,并横向排列显示出来,注意,是横向,每行8个,自动换行。高手请进来看看.
这些大类和小类的所属关系都在我的一个sql表里。使用datagrid查询数据表并显示我会,但是会竖向排列,现在想横向排列,高手有什么好方法呢?

解决方案 »

  1.   

    用DataList,
    DataList的格试比较灵活。
    RepeatColumns="8"
    其它的和datagrid用法一样
      

  2.   

    datalist 或者使用表格控件
      

  3.   

    DataList,DataList比较灵活RepeatColumns="8"
    其它的和datagrid差不了多少,都是一样的
      

  4.   

    高手们,我用了datalist控件,RepeatColumns="8",我也设置了,可执行下面的代码并没有把数据表的内容添加到这个控件里啊??以下是我的代码: SqlConnection conn=new SqlConnection();
    conn.ConnectionString="data source =220.201.132.55;initial catalog=word;persist security info=false;uid=sa;pwd=longwang;";
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText="select * from 国家";
    cmd.Connection=conn;
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=cmd;
    DataSet ds=new DataSet();
    da.Fill(ds,"sa");
    DataList1.DataSource=ds.Tables[0].DefaultView;
    DataList1.DataBind();
      

  5.   

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data.SqlClient" %><script runat=server>void Page_Load(Object sender , EventArgs e) 
    {
    if (! IsPostBack)
    {
    BindDataList();
    }
    }void BindDataList (){
    SqlConnection conNorthwind;
    SqlCommand cmdSelect;
    SqlDataReader dtrCategories; conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
    cmdSelect = new SqlCommand( "Select CategoryID, CategoryName From Categories", conNorthwind );
    conNorthwind.Open();
    dtrCategories = cmdSelect.ExecuteReader(); dlstCategories.DataSource = dtrCategories;
    dlstCategories.DataBind(); dtrCategories.Close();
    conNorthwind.Close();
    }void BindRepeater( int intCatID ) {
    SqlConnection conNorthwind;
    string  strSelect;
    SqlCommand cmdSelect;
    SqlDataReader dtrProducts; conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
    strSelect = "Select ProductName From Products Where CategoryID=@catID";
    cmdSelect = new SqlCommand( strSelect, conNorthwind );
    cmdSelect.Parameters.Add( "@catID", intCatID );
    conNorthwind.Open();
    dtrProducts = cmdSelect.ExecuteReader(); rptProducts.DataSource = dtrProducts;
    rptProducts.DataBind(); dtrProducts.Close();
    conNorthwind.Close();
    }void dlstCategories_ItemCommand( object s, DataListCommandEventArgs e ) {
    int intCatID;
     
    dlstCategories.SelectedIndex = e.Item.ItemIndex;
    BindDataList();
    intCatID = (int)dlstCategories.DataKeys[(int)e.Item.ItemIndex];
    BindRepeater( intCatID );
    }</Script><html>
    <head><title>DataListMasterDetail.aspx</title></head>
    <body>
    <form Runat="Server"><table width="100%">
    <tr><td valign="top"><asp:DataList
      ID="dlstCategories"
      OnItemCommand="dlstCategories_ItemCommand"
      DataKeyField="CategoryID"
      RepeatColumns="8"    //每行显示8个记录
      RepeatDirection="Horizontal"   //横向排序
      ItemStyle-BorderStyle="Solid"
      ItemStyle-BorderColor="Blue"
      Width="200"
      CellPadding="5"
      CellSpacing="10"
      BackColor="lightgrey"
      Runat="Server"><ItemTemplate>
      <asp:LinkButton
        Text='<%#DataBinder.Eval(Container.DataItem, "CategoryName" )%>'
        Runat="Server" />
    </ItemTemplate><SelectedItemTemplate>
      <b><i><%#DataBinder.Eval(Container.DataItem, "CategoryName" )%></i></b>
    </SelectedItemTemplate></asp:DataList></td><td valign="top" width="100%"><asp:Repeater
      ID="rptProducts"
      Runat="Server"><ItemTemplate>
      <%#DataBinder.Eval(Container.DataItem,  "ProductName" ) %>
    </ItemTemplate><SeparatorTemplate>
      <hr>
    </SeparatorTemplate></asp:Repeater></td></tr>
    </table></form>
    </body>
    </html>
      

  6.   

    楼主﹐你的DataList没有显示数据﹐是因为DataList的模板没有帮定到数据﹐看楼上的DataList在Html部分的邦定
    <ItemTemplate>
    <asp:LinkButton
    Text='<%#DataBinder.Eval(Container.DataItem, "CategoryName" )%>'
    Runat="Server" />
    </ItemTemplate><SelectedItemTemplate>
    <b><i><%#DataBinder.Eval(Container.DataItem, "CategoryName" )%></i></b>
    </SelectedItemTemplate>
      

  7.   

    这样效率太低.应该把大类小类一次都读出来,生成相应的js脚本,用javascript控制他们的关系就好了.
      

  8.   

    这些类不多还不如直接输出html代码来得快