Choose a location:
posted 5/16/2010 by mohit kumar
In this blog I will explain how to find control in nested Data list. I will also tell you how you can bind nested Data list.In below code there is a class file (DummyData.cs) and a page (NestedDatalist.aspx).Here is my code:-
NestedDatalist.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedDatalist.aspx.cs" Inherits="NestedDatalist" %> <!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 id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList runat="server" ID="dlParent" BorderWidth="6" OnItemDataBound="dlParent_ItemDataBound"> <ItemTemplate> <asp:Literal ID="litParent" runat="server" Text=' <%#Eval("Header")%>'></asp:Literal> <asp:DataList runat="server" ID="dlChild" BorderWidth="4" OnItemDataBound="dlChild_ItemDataBound"> <ItemTemplate> <asp:Literal ID="litChild" runat="server" Text=' <%#Eval("Header")%>'></asp:Literal> <asp:DataList runat="server" ID="dlSubChild" BorderWidth="2" runat="server" > <ItemTemplate> <table> <tr> <td><asp:Literal ID="litSubChild" runat="server" Text=' <%#Eval("Header")%>'></asp:Literal></td> </tr> <tr> <td>Enter your Name:</td> <td><asp:TextBox runat="server" ID="txtName" ></asp:TextBox></td> </tr> <tr> <td colspan="2"><asp:Button ID="btnfind" Text="Click" runat="server" OnClick="btnfind_Click" /></td> </tr> </table> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> <asp:Literal ID="litName" runat="server"></asp:Literal> </div> </form> </body> </html>
NestedDatalist.aspx.cs
using System; using System.Web.UI.WebControls; public partial class NestedDatalist : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //call BindParent Method BindParent(); } } private void BindParent() { //binding parent Data list dlParent.DataSource = DummyData.GetData("Parent Header"); dlParent.DataBind(); } protected void dlParent_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { //here I am finding item(DataList) of parent Datalist DataList dlChild = (DataList)e.Item.FindControl("dlChild"); //binding child Data list dlChild.DataSource = DummyData.GetData("Child Header"); dlChild.DataBind(); } } protected void dlChild_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { //here I am finding item(DataList) of child Datalist DataList dlSubChild = (DataList)e.Item.FindControl("dlSubChild"); //binding sub child Data list dlSubChild.DataSource = DummyData.GetData("Sub Header"); dlSubChild.DataBind(); } } protected void btnfind_Click(object sender, EventArgs e) { foreach (DataListItem parentItem in dlParent.Items) { //here I am finding item(DataList) of parent Datalist DataList dlChild = (DataList)parentItem.FindControl("dlChild"); foreach (DataListItem childItem in dlChild.Items) { //here I am finding item(DataList) of child Datalist DataList dlSubChild = (DataList)childItem.FindControl("dlSubChild"); foreach (DataListItem subChildItem in dlSubChild.Items) { //here I am finding item(TextBox) of sub child Datalist TextBox txtName = (TextBox)subChildItem.FindControl("txtName"); //set literal(litName) text litName.Text = string.Format("{0}{1}", "Welcome ", txtName.Text); } } } } }
DummyData.cs
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Web; public class DummyData { private string _header; public string Header { get { return _header; } set { _header = value; } } public static Collection<DummyData> GetData(string header) { Collection<DummyData> list = new Collection<DummyData>(); DummyData dummyData = new DummyData(); dummyData.Header = header; list.Add(dummyData); return list; } }
Happy Coding:)
This comment is awaiting moderation.
Hi.. this Code working ...but how to find second datalist's label in first datalist's databound event.. can u plz help me...
What kind of email newsletter would you prefer to receive from CodeAsp.Net? 18