📄 masterdetail.aspx
字号:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat=server>
void Page_Load(Object sender , EventArgs e)
{
if (! IsPostBack ) {
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();
dropCategories.DataSource = dtrCategories;
dropCategories.DataTextField = "CategoryName";
dropCategories.DataValueField = "CategoryID";
dropCategories.DataBind();
dropCategories.Items.Insert( 0, new ListItem( "none selected", "-1" ) );
dtrCategories.Close();
conNorthwind.Close();
}
}
void BindProducts( int intCatID ) {
SqlConnection conNorthwind;
SqlCommand cmdSelect;
string strSelect;
SqlDataReader dtrProducts;
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
conNorthwind.Open();
strSelect = "Select ProductName, UnitPrice From Products Where CategoryID=@catID";
cmdSelect = new SqlCommand( strSelect, conNorthwind );
cmdSelect.Parameters.Add( "@catID", intCatID );
dtrProducts = cmdSelect.ExecuteReader();
rptProducts.DataSource = dtrProducts;
rptProducts.DataBind();
dtrProducts.Close();
conNorthwind.Close();
}
void dropCategories_SelectedIndexChanged( object s, EventArgs e ) {
int intCatID = System.Convert.ToInt32(dropCategories.SelectedItem.Value);
if ( intCatID != -1 ) {
BindProducts( intCatID );
}
}
</Script>
<html>
<head><title>MasterDetail.aspx</title></head>
<body>
<form Runat="Server">
<b>Select Category:</b>
<asp:DropDownList
ID="dropCategories"
AutoPostBack="True"
OnSelectedIndexChanged="dropCategories_SelectedIndexChanged"
Runat="Server"/>
<p>
<asp:Repeater
ID="rptProducts"
EnableViewState="False"
Runat="Server">
<ItemTemplate>
<li><%#DataBinder.Eval(Container.DataItem, "ProductName" ) %>
- <%# DataBinder.Eval( Container.DataItem, "UnitPrice" ) %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -