La pagina aspx è:
Mentre il codebehind:codice:<form id="Form1" method="post" runat="server"> <asp:DataList id="DataList1" runat="server"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Categoryname") %> <asp:DataList id="DataList2" runat="server"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Productname") %> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </form>
codice:Imports System.Data Imports System.Data.SqlClient Public Class WebForm5 Inherits System.Web.UI.Page Dim dtProdotti As New DataTable("Products") #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents DataList1 As System.Web.UI.WebControls.DataList Protected WithEvents DataList2 As System.Web.UI.WebControls.DataList 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Page.IsPostBack Then GetProdotti() bindDataList() End If End Sub Private Sub bindDataList() Dim cnn As New SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost") Dim cmd As New SqlCommand("SELECT * FROM Categories", cnn) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable("Categories") cnn.Open() da.Fill(dt) cnn.Close() Me.DataList1.DataSource = dt Me.DataList1.DataBind() End Sub Private Sub GetProdotti() Dim cnn As New SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost") Dim cmd As New SqlCommand("SELECT * FROM Products", cnn) Dim da As New SqlDataAdapter(cmd) 'Dim dt As New DataTable("Products") cnn.Open() da.Fill(dtProdotti) cnn.Close() End Sub Private Sub bindDataList2(ByVal i As String, ByRef dl As DataList) Dim dv As DataView dv = dtProdotti.DefaultView dv.RowFilter = "CategoryID = " & i dl.DataSource = dv dl.DataBind() End Sub Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound Dim d As DataRowView d = e.Item.DataItem Dim d2 As DataList d2 = e.Item.FindControl("DataList2") bindDataList2(d(0), d2) End Sub End Class
Fammi sapere
Ciao
Kalman

Rispondi quotando