📄 datalistmasterdetail.aspx
字号:
<%@ 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"
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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -