|
This example will display the all the categories in the category tables in data list control. When the user clicks on any category, the corresponding products detail will be displayed in the repeater controls.
MasterDetail.aspx
<% @ Page Language ="VB" AutoEventWireup ="false" CodeFile ="Example13.aspx.vb" Inherits ="Example13" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml">
<head runat ="server">
</head>
<body>
<form id ="form1" runat ="server">
<table>
<tr>
<td>
<asp:DataList ID ="DataList1" runat ="server">
<ItemTemplate>
<asp:LinkButton Text =' <% # Container.DataItem("CategoryName") %>' runat ="server" />
</ItemTemplate>
</asp:DataList>
</td><td>
<asp:Repeater ID ="Repeater1" runat ="server">
<ItemTemplate>
<% # Container.DataItem( "ProductName" ) %>
</ItemTemplate>
<SeparatorTemplate><hr>
</SeparatorTemplate>
</asp:Repeater>
</td></tr></table>
</form>
</body>
</html>
MasterDetail.aspx.vb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Example13 Inherits System.Web.UI.Page
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
If Not IsPostBack Then
BindDataList()
End If
End Sub
Sub BindDataList()
Dim con As SqlConnection
Dim ada As SqlDataAdapter
Dim dst As DataSet
con = New SqlConnection( "Data Source=localhost;Initial catalog=Northwind;UID=sa;pwd=sa" )
ada = New SqlDataAdapter( "select CategoryId,CategoryName from Categories" , con)
dst = New DataSet
con.Open()
ada.Fill(dst, "Categories" )
con.Close()
DataList1.DataSource = dst.Tables( "Categories" )
DataList1.DataKeyField = "CategoryId"
DataList1.DataBind()
End Sub
Sub BindRepeater( ByVal intCatId As Integer )
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dtr As SqlDataReader
con = New SqlConnection( "Data Source=localhost;Initial catalog=Northwind;UID=sa;pwd=sa" )
cmd = New SqlCommand( "Select ProductName from Products where CategoryId=@catId" , con)
cmd.Parameters.Add( "@catId" , SqlDbType.Int).Value= intCatId
con.Open()
dtr = cmd.ExecuteReader
Repeater1.DataSource = dtr
Repeater1.DataBind()
dtr.Close()
con.Close()
End Sub
Protected Sub DataList1_ItemCommand( ByVal source As Object , ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
Dim categoryId As Integer
DataList1.SelectedIndex = e.Item.ItemIndex
BindDataList()
categoryId = DataList1.DataKeys(e.Item.ItemIndex)
BindRepeater(categoryId)
End Sub
End Class |