Nested ListView in Asp.Net
Today In this Article, I am trying to show you that how can we use nested list view control in asp.net. For example, suppose we want to display some categories and subcategories of any records. The
following demonstration will show you how to perform this task.
Code: DemoListview.aspx
<asp:ListView runat="server" ID="ListView1" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">
<LayoutTemplate>
<table runat="server" id="table1" cellpadding="5" bgcolor="#E0E0E0" width="100">
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate><%-- sets the custom content for the data item in a ListView control.--%>
<tr id="Tr1" runat="server">
<td id="Td1" runat="server" align="center">
<asp:Label ID="NameLabel" runat="server" Font-Size="Large" Text='<%#Eval("Cat") %> ' />
<hr />
<asp:ListView ID="ListView2" runat="server" DataSourceID="SubCategory">
<LayoutTemplate>
<table runat="server" id="table2">
<tr runat="server" id="itemPlaceHolder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server" align="left">
<asp:Label ID="LabelSubCat" runat="server" Text='<%#Eval("SubCat")%>'
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SubCategory" runat="server"></asp:SqlDataSource><%--SqlDataSource control use for database connectivity to retrieve SubCategory list --%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource><%--SqlDataSource control use for database connectivity to retrieve Category list--%>
Code: DemoListview.aspx.cs
public partial class DemolListview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.ConnectionString = "Data Source=aaa; initial catalog = Detail; User id=bbb; password=cccc;";//creating connection from database
SqlDataSource1.SelectCommand = "Select * from Category";//Executing sql query
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label lblID = e.Item.FindControl("NameLabel") as Label;//finding control in Listview1 control
SqlDataSource sqlds = e.Item.FindControl("SubCategory") as SqlDataSource;//finding sqldatasource control in Listview1 control
sqlds.ConnectionString = " Data Source=aaa; initial catalog = Detail; User id=bbb; password=cccc;";//creating connection
sqlds.SelectCommand = "select SubCat from SubCategory where Cat='" + lblID.Text + "'";";//Executing sql query
}
When we will run the application it will show .Net and Database categories along with their subcategories as shown below:
Output
Anonymous User
09-Apr-2019Thanks for sharing.
John Smith
19-Oct-2014Check your connection String...
http://www.mindstick.com/Articles/d49d88a2-5a67-426f-87a0-00e16bd04f62/Connection%20Strings
http://www.mindstick.com/Articles/42afb9ee-163f-42cd-8a74-563203d58e8f/Connection%20String
Aarti Yadav
18-Oct-2014Object reference not set to an instance of an object.
Anurag Chaurasia
03-Jun-2011