📄 datalistmasterdetail.aspx
字号:
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
If Not IsPostBack
BindDataList
End If
End Sub
Sub BindDataList
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrCategories As SqlDataReader
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;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()
End Sub
Sub BindRepeater( intCatID As Integer )
Dim conNorthwind As SqlConnection
Dim strSelect As String
Dim cmdSelect As SqlCommand
Dim dtrProducts As SqlDataReader
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;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()
End Sub
Sub dlstCategories_ItemCommand( s As Object, e As DataListCommandEventArgs )
Dim intCatID As Integer
dlstCategories.SelectedIndex = e.Item.ItemIndex
BindDataList
intCatID = dlstCategories.DataKeys( e.Item.ItemIndex )
BindRepeater( intCatID )
End Sub
</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='<%# Container.DataItem( "CategoryName" )%>'
Runat="Server" />
</ItemTemplate>
<SelectedItemTemplate>
<b><i><%# Container.DataItem( "CategoryName" )%></i></b>
</SelectedItemTemplate>
</asp:DataList>
</td><td valign="top" width="100%">
<asp:Repeater
ID="rptProducts"
Runat="Server">
<ItemTemplate>
<%# 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 + -