???菜鸟刚学用c#做asp.net网页,我想实现以下功能。我点了一个大类,比如农业,在新打开的网页上去数据库的表里查询属于农业的小类来,并横向排列显示出来,注意,是横向,每行8个,自动换行。高手请进来看看.
这些大类和小类的所属关系都在我的一个sql表里。使用datagrid查询数据表并显示我会,但是会竖向排列,现在想横向排列,高手有什么好方法呢?
这些大类和小类的所属关系都在我的一个sql表里。使用datagrid查询数据表并显示我会,但是会竖向排列,现在想横向排列,高手有什么好方法呢?
DataList的格试比较灵活。
RepeatColumns="8"
其它的和datagrid用法一样
其它的和datagrid差不了多少,都是一样的
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();
<%@ 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>
<ItemTemplate>
<asp:LinkButton
Text='<%#DataBinder.Eval(Container.DataItem, "CategoryName" )%>'
Runat="Server" />
</ItemTemplate><SelectedItemTemplate>
<b><i><%#DataBinder.Eval(Container.DataItem, "CategoryName" )%></i></b>
</SelectedItemTemplate>