Monday, March 14, 2011

Nested Datalist in asp.net c# , Item Boutnd In Nested Datalist




.Aspx Page
 <asp:DataList ID="parentDataList" CellPadding="10" CellSpacing="10" RepeatDirection="horizontal"
                            runat="server" DataKeyField="id">
                            <ItemTemplate>
                                <strong>
                                    <asp:Label CssClass="main-cotent" ID="idLabel" runat="server" Text='<%# Eval("category") %>' /></strong>
                                <asp:DataList ID="nestedDataList" runat="server" RepeatDirection="vertical">
                                    <ItemTemplate>
                                        <ul class="ul-div li" style="margin: 0px; padding: 5px px 5px 5px">
                                            <li><a href='product.aspx?cate=<%#Eval("category")%>&subcate=<%#Eval("subcategory")%>'>
                                                <asp:Label ID="Label" runat="server" Text='<%# Eval("subcategory") %>'></asp:Label></a></li></ul>
                                    </ItemTemplate>
                                </asp:DataList>
                            </ItemTemplate>
                        </asp:DataList>

.aspx.cs Page


public void BindParentDataList()
    {
        // string variable to store the connection string
        // retrieved from the connectionStrings section of web.config


        // sql command object initialized with select command text
        SqlCommand mySqlCommand = new SqlCommand("select * from tbl_category", connection);

        // check the connection state and open it accordingly.
        if (connection.State == ConnectionState.Closed)
            connection.Open();

        // Sql datareader object to read the stream of rows from SQL Server Database
        SqlDataReader myDataReader = mySqlCommand.ExecuteReader();

        // Pass the Sql DataReader object to the DataSource property
        // of DataList control to render the list of items.
        parentDataList.DataSource = myDataReader;
        parentDataList.DataBind();

        // close the Sql DataReader object
        myDataReader.Close();

        // check the connection state and close it accordingly.
        if (connection.State == ConnectionState.Open)
            connection.Close();

        // foreach loop over each item of DataList control
        foreach (DataListItem Item in parentDataList.Items)
        {

            BindNestedDataList();
        }

    }

public void BindNestedDataList()
    {

        int i;
        for (i = 0; i < parentDataList.Items.Count; i++)
        {
            // get CategoryID value for the current datalist item
            // DataKeys collection object returns the associated value
            // at specified Item Index of DataList
            Label lit = (Label)parentDataList.Items[i].FindControl("idLabel");

            // string variable to store the connection string
            // retrieved from the connectionStrings section of web.config
            //string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

            //// sql connection object
            //SqlConnection mySqlConnection = new SqlConnection(connectionString);

            // sql command object initialized with select command text
            SqlCommand mySqlCommand = new SqlCommand("select * from tbl_subcategoryname where category='" + lit.Text + "'", connection);



            // check the connection state and open it accordingly.
            if (connection.State == ConnectionState.Closed)
                connection.Open();

            // Sql datareader object to read the stream of rows from SQL Server Database
            SqlDataReader myDataReader = mySqlCommand.ExecuteReader();

            // findControl function to get the nested datalist control
            DataList nestedDataList = (DataList)parentDataList.Items[i].FindControl("nestedDataList");

            nestedDataList.DataSource = myDataReader;
            nestedDataList.DataBind();

            // close the Sql DataReader object
            myDataReader.Close();

            // check the connection state and close it accordingly.
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }