Home > DeveloperSection > Articles > Nested ListView in Asp.Net

Nested ListView in Asp.Net


ASP.Net ASP.Net 
Ratings:
3 Comment(s)
 32170  View(s)
Rate this:

Nested ListView in Asp.Net

In this demonstration I am going 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 sub categories of any records. Following demonstration will show you that 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:

Ouput

Nested ListView in Asp.Net

 


Great Work Sir

By Anurag Chaurasia on   5 years ago
Now i knows that we can execute a stored procedure too from the sqldatasource control.......

geting a error

By Aarti Yadav on   2 years ago

Object reference not set to an instance of an object.


what to know..
Line 83:                 Label lblID = e.Item.FindControl("id_Label") as Label;
Line 84:                 SqlDataSource sqlds = e.Item.FindControl("SqlDataSource3") as SqlDataSource;
Line 85:                 sqlds.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
Line 86:                 sqlds.SelectCommand = "select comment,rply_name,rpl_date,task_id from rply where task_id ='" + lblID.Text + "'";


Don't want to miss updates? Please click the below button!

Follow MindStick