Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,

I am created a project in which i am having two datalist in nested manner i.e(datalist within a datalist), i am getting the values from database for both the datalist. In the child datalist i am having checkbox, if i check the checkbox and click the button which is out of datalist the checked items has to be stored into the database.In breakpoint the checked item is showing checked = false so the checked item is not getting inserted into the database. please help me asap.

here is my aspx

XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
function divexpandcollapse(divname) {
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);

if (div.style.display == "none") {
div.style.display = "inline";
img.src = "minus.gif";
} else {
div.style.display = "none";
img.src = "plus.gif";
}
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

            <asp:DataList ID="outerDataList" runat="server" OnItemDataBound="outerRep_ItemDataBound">
                <ItemTemplate>
                    <a href="JavaScript:divexpandcollapse('div<%# Eval("BUSINESSUPLOAD_SERVICES_SERVICENAME") %>');">
<img id="imgdiv<%# Eval("BUSINESSUPLOAD_SERVICES_SERVICENAME") %>" width="9px" border="0" src="plus.gif" />
</a>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("BUSINESSUPLOAD_SERVICES_SERVICENAME") %>' />
                   <tr>
    <td>
    <div id="div<%# Eval("BUSINESSUPLOAD_SERVICES_SERVICENAME") %>" style="display: none; position: relative; left: 15px; overflow: auto; float:right">

                        <asp:DataList ID="innerDataList" runat="server" RepeatDirection="Horizontal">
                            <ItemTemplate>
    <%--<table cellpadding="5px" cellspacing="5px">
    <tr>
    <td>--%>
    <asp:CheckBox ID="dlchk" runat="server" Text='<%#Eval("BUSINESSUPLOAD_SUBSERVICES_SUB_SERVICENAME") %>' />
   <%-- </td>
    </tr>
    </table>--%>
    </ItemTemplate>
                        </asp:DataList>

                </ItemTemplate>
            </asp:DataList>

    </div>
    <asp:Button ID="btn1" runat="server" Text="Submit" onclick="btn1_Click" />
    </form>
</html>


here is my cs

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    string str = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
           BindData();
    }
    private void BindData()
    {
       
       SqlConnection cnn = new SqlConnection("Data Source=DOTNET-SYS8\\SQLEXPRESS;Initial Catalog=treatmentareas;Integrated Security=True");
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from MEDICALVOYAGER_BUSINESSUPLOAD_SERVICES", cnn);


            DataSet ds = new DataSet();
            cmd1.Fill(ds, "services");


            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from MEDICALVOYAGER_BUSINESSUPLOAD_SUBSERVICES as sub,MEDICALVOYAGER_BUSINESSUPLOAD_SERVICES as serv where sub.BUSINESSUPLOAD_SERVICES_SERVICENAME=serv.BUSINESSUPLOAD_SERVICES_SERVICENAME", cnn);
            cmd2.Fill(ds, "subservices");


            ds.Relations.Add("myrelation", ds.Tables["services"].Columns["BUSINESSUPLOAD_SERVICES_SERVICENAME"], ds.Tables["subservices"].Columns["BUSINESSUPLOAD_SERVICES_SERVICENAME"]);
       
        outerDataList.DataSource = ds.Tables["services"];
        outerDataList.DataBind();
    }
 
    protected void outerRep_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            DataList innerDataList = e.Item.FindControl("innerDataList") as DataList;
            innerDataList.DataSource = drv.CreateChildView("myrelation");
           

            innerDataList.DataBind();
        }
    }
    //protected void innerDl_ItemDataBound(object sender, DataListItemEventArgs e)
    //{

    //    var ck = e.Item.FindControl("dlchk") as CheckBox;
    //    if (ck != null)
    //    {
    //        ck.ID = ck.Text;
    //        ck.Text = "";
    //        if (IsPostBack && Request.Form[ck.UniqueID] != null)
    //        {
    //            ck.Checked = true;
    //        }
    //        if (ck.Checked == true)
    //        {
    //            str = str + ck.Text + ",";
    //        }
    //    }
            

    
    //}



    protected void btn1_Click(object sender, EventArgs e)
    {
        servicesselected();
        SqlConnection cnn = new SqlConnection("Data Source=DOTNET-SYS8\\SQLEXPRESS;Initial Catalog=MEDICALVOYAGER;Integrated Security=True");
        cnn.Open();
        SqlCommand com = new SqlCommand("insert into MEDICALVOYAGER_BUSINESSUPLOAD_SERVICES_SELECTED(BUSINESSUPLOAD_SUBSERVICES) values (@subservices)", cnn);
        
        com.Parameters.AddWithValue("@subservices", str);
       
        com.ExecuteNonQuery();
        Response.Write(str);
        cnn.Close();
    }
    protected void servicesselected()
    {
        //int itemCount1 = 0;
        //for (itemCount1 = 0; itemCount1 < outerDataList.Items.Count; itemCount1++)
        //{
        //    CheckBox chk = (CheckBox)outerDataList.Items[0].FindControl("innerDataList").FindControl("dlchk");
        //    if (chk.Checked!=false)
        //    {
        //        str = str + chk.Text + ",";
        //    }
        //}


        int itemCount1 = 0;
        for (itemCount1 = 0; itemCount1 < outerDataList.Items.Count; itemCount1++)
        {

            DataList childdl = outerDataList.Items[itemCount1].FindControl("innerDataList") as System.Web.UI.WebControls.DataList;
            foreach (DataListItem dl in childdl.Items)
            {
                CheckBox chk = (CheckBox)dl.FindControl("dlchk");

                if (chk != null)
                {
                    chk.ID = chk.Text;
                    chk.Text = "";
                    if (IsPostBack && Request.Form[chk.UniqueID] != null)
                    {
                        chk.Checked = true;
                    }
                    if (chk.Checked == true)
                    {
                        str = str + chk.Text + ",";
                    }
                }

                //if (chk.Checked != false)
                //{
                //    str = str + chk.Text + ",";
                //}
            }
        }
            //for (int itemCount = 0; itemCount < childdl.Items.Count; itemCount++)
            //{

            //    CheckBox chkbox1 = ((CheckBox)childdl.Items[itemCount].FindControl("dlchk"));

            //    if (chkbox1.Checked != false)
            //    {
            //        str = str + chkbox1.Text + ",";
            //    }
                

            //}


        }
}

please help me.
Posted
Updated 30-Jul-12 22:06pm
v3

1 solution

You could check out this article:
A quick guide to using nested repeaters in ASP.NET[^]
 
Share this answer
 
Comments
shabadiveda 31-Jul-12 21:59pm    
my problem is i have to get the child items in horizontal directiion, if i use repeates i cant get the items in horizontal direction. I am achieving that by using repeatdirection property of datalist.
Any suggestion to make horizontal direction?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900