locked
Dynamicall created checkboxList shows null in FindControl RRS feed

  • Question

  • User69848886 posted

    hi, i have created many checboxklist dynamically and also in every checkboxList there have many listitem and few are checked by depend on data from  database.i print everything inside a div.but my problem is that when i use findcontrol for checkboxlist i get null.

    i create checkboxlist is PostBack.

    this is my UI design

     <div class="panel-body">
         <div id="advancedcommitteediv" runat="server">
         </div>
      </div>

    my generat control code

     Committees c = new Committees();
                            c = new CommitteesDA().GetCommitteeByID(Convert.ToInt32(crd.Committee_ID));
                            if (c != null)
                            {
                                html += "<div class='panel panel-default'>";
                                html += "<div class='panel-heading'>" + c.Name.ToString() + "</div>";
                                html += "<div class='panel-body'>";
                                html += "<div style='border: 1px solid #ccc;margin-bottom: 10px'>";
                                html += "<label class='activeroster'><span style='margin-left:10px;'>" + crd.Delegations.Name.ToString() + "</span><span style='margin-left:50px;'>Max ( " + c.DefaultMaxStudentsPerDelegation + " ) Student</span></label>";
                                html += "<div>" + checkboxlistfor(i, Convert.ToInt32(crd.Delegation_ID), Convert.ToInt32(c.DefaultMaxStudentsPerDelegation), Convert.ToInt32(c.ID)) + "</div>";
                                html += "</div>";
    
                            }
                            Session["currentcommittee"] = crd.Committee_ID;
    
                        }
    
    
                    }
                    html += "</div>";
                    html += "</div>";
                }
                advancedcommitteediv.InnerHtml = html;

    and this is the checkboxlistfor function

    public string checkboxlistfor(int i, int delegationid, int limit, int committeeid)
        {
            try
            {
                StringWriter sw = new StringWriter();
                div = new HtmlGenericControl("DIV");
                div.Attributes.Add("id", "innercontrol" + i);
               
                CheckBoxList chki = new CheckBoxList();
                chki = new CheckBoxList();
                chki.Items.Clear();
                chki.ID = "chk" + i;
                chki.Attributes.Add("onclick", "CheckCheck(" + chki.ID + "," + limit + ")");
    
                //fDynamicControls.Add(chki.ID, chki);
    
                HiddenField hd = new HiddenField();
                hd.ID = "hdComittee" + i;
                hd.Value = committeeid.ToString();
                div.Controls.Add(hd);
                hd = new HiddenField();
                hd.ID = "hdDelegation" + i;
                hd.Value = delegationid.ToString();
                div.Controls.Add(hd);
                List<User> objstudent = new List<User>();
                //apply checking for admin
                objstudent = new UserDA(true, true).Getstudentbyschoolid(Convert.ToInt32(((User)Session["UserObject"]).School_ID));
                objstudent = objstudent.Where(x => x.GraduationYear >= DateTime.Now.Year || x.GraduationYear <= 0).OrderBy(x => x.LastName).ToList();
                objstudent = objstudent.OrderBy(x => x.FirstName).ToList();           
                List<UserConferenceSettingEntries> objuserconfsettingentries = new List<UserConferenceSettingEntries>();
                List<UserConferenceSettingEntries> objuserconfsettingentriesoldList = new List<UserConferenceSettingEntries>();
                objuserconfsettingentries = new UserConferenceSettingEntriesDA(true, true).GetAllStudent_conferenceid(Convert.ToInt32(Session["conferenceid"]));
                objuserconfsettingentries = objuserconfsettingentries.ToList();
                Session["objuserconfsettingentries"] = objuserconfsettingentries;
                if (objstudent.Count > 0)
                {
                    foreach (User u in objstudent)
                    {
                        UserConferenceSettingEntries objcon = objuserconfsettingentries.Where(x => x.User_ID == u.ID).FirstOrDefault();
                        if (objcon != null)
                        {
                            i++;
                            ListItem ltt = new ListItem();
                            string Name = "";
                            Name = u.FirstName.ToString() + " " + u.LastName.ToString();
                            ltt.Text = Name.ToString().Length > 15 ? (Name.Substring(0, 15) + "..") : (Name.ToString());
                            ltt.Value = u.ID.ToString();
                            if (objcon.Delegation_ID == delegationid)
                            {
                                ltt.Selected = true;
                            }
                            else
                            {
                                ltt.Selected = false;
                            }
    
                            ltt.Attributes["title"] = Name.ToString();
    
                            if (u.Gender == "M")
                            {
                                ltt.Attributes.Add("Style", "color: #99CCFF;");
                            }
                            else
                            {
                                ltt.Attributes.Add("Style", "color: #ff9aa4;");
                            }
                            chki.Items.Add(ltt);
                            // objuserconfsettingentriesoldList.Add(objcon);
    
                        }
    
                    }
                    //Session["objuserconfsettingentriesoldList"] = objuserconfsettingentriesoldList;
    
                }
                else
                {
    
    
                }
              
              
                div.Controls.Add(chki);
    
                div.RenderControl(new HtmlTextWriter(sw));
                string str = sw.ToString();
                return str;
    
            }
            catch (Exception ex)
            {
                return "";
            }
    
        }

    in button click i get the checkboxlist

     CheckBoxList chkboxlist = (CheckBoxList)advancedcommitteediv.FindControl("chk" + i);

    but it show null???

    this is the HTML generated

    <div class="panel-body">
         <div id="ctl00_Body_advancedcommitteediv"><
    div class="panel panel-default"><
    div class="panel-heading">Secretariat</div><
    div class="panel-body"><
    div style="border: 1px solid #ccc;margin-bottom: 10px"><label class="activeroster"><span style="margin-left:10px;">Algeria</span><span style="margin-left:50px;">Max ( 1 ) Student</span></label><
    div><
    div id="innercontrol1"><input type="hidden" value="19" id="hdComittee1" name="hdComittee1"><input type="hidden" value="3" id="hdDelegation1" name="hdDelegation1"><
    table border="0" onclick="CheckCheck(chk1,1)" id="chk1">
    	<tbody><tr>
    		<td><span style="color: #99CCFF;" title="Aftab duza"><input type="checkbox" name="chk1$0" id="chk1_0"><label for="chk1_0">Aftab duza</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Amin Islam"><input type="checkbox" name="chk1$1" id="chk1_1"><label for="chk1_1">Amin Islam</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Arifa katon"><input type="checkbox" checked="checked" name="chk1$2" id="chk1_2"><label for="chk1_2">Arifa katon</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Bappy das"><input type="checkbox" name="chk1$3" id="chk1_3"><label for="chk1_3">Bappy das</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="hasem amla"><input type="checkbox" name="chk1$4" id="chk1_4"><label for="chk1_4">hasem amla</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Katrina kaif"><input type="checkbox" name="chk1$5" id="chk1_5"><label for="chk1_5">Katrina kaif</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="masum khan"><input type="checkbox" name="chk1$6" id="chk1_6"><label for="chk1_6">masum khan</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Rabiul Hossain"><input type="checkbox" name="chk1$7" id="chk1_7"><label for="chk1_7">Rabiul Hossain</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Shahid Ullah"><input type="checkbox" name="chk1$8" id="chk1_8"><label for="chk1_8">Shahid Ullah</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="sumon mia"><input type="checkbox" name="chk1$9" id="chk1_9"><label for="chk1_9">sumon mia</label></span></td>
    	</tr>
    </tbody></table></div></div></div><
    div style="border: 1px solid #ccc;margin-bottom: 10px"><label class="activeroster"><span style="margin-left:10px;">American Samoa</span><span style="margin-left:50px;">Max ( 1 ) Student</span></label><div><
    div id="innercontrol2"><input type="hidden" value="19" id="hdComittee2" name="hdComittee2"><input type="hidden" value="4" id="hdDelegation2" name="hdDelegation2"><
    table border="0" onclick="CheckCheck(chk2,1)" id="chk2">
    	<tbody><tr>
    		<td><span style="color: #99CCFF;" title="Aftab duza"><input type="checkbox" name="chk2$0" id="chk2_0"><label for="chk2_0">Aftab duza</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Amin Islam"><input type="checkbox" name="chk2$1" id="chk2_1"><label for="chk2_1">Amin Islam</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Arifa katon"><input type="checkbox" name="chk2$2" id="chk2_2"><label for="chk2_2">Arifa katon</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Bappy das"><input type="checkbox" checked="checked" name="chk2$3" id="chk2_3"><label for="chk2_3">Bappy das</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="hasem amla"><input type="checkbox" name="chk2$4" id="chk2_4"><label for="chk2_4">hasem amla</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Katrina kaif"><input type="checkbox" name="chk2$5" id="chk2_5"><label for="chk2_5">Katrina kaif</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="masum khan"><input type="checkbox" name="chk2$6" id="chk2_6"><label for="chk2_6">masum khan</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Rabiul Hossain"><input type="checkbox" name="chk2$7" id="chk2_7"><label for="chk2_7">Rabiul Hossain</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Shahid Ullah"><input type="checkbox" name="chk2$8" id="chk2_8"><label for="chk2_8">Shahid Ullah</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="sumon mia"><input type="checkbox" name="chk2$9" id="chk2_9"><label for="chk2_9">sumon mia</label></span></td>
    	</tr>
    </tbody></table></div></div></div></div></div><div class="panel panel-default"><div class="panel-heading">G8 Summit of 2005</div><div class="panel-body"><div style="border: 1px solid #ccc;margin-bottom: 10px"><label class="activeroster"><span style="margin-left:10px;">Bahrain</span><span style="margin-left:50px;">Max ( 1 ) Student</span></label><div><div id="innercontrol3"><input type="hidden" value="61" id="hdComittee3" name="hdComittee3"><input type="hidden" value="17" id="hdDelegation3" name="hdDelegation3"><table border="0" onclick="CheckCheck(chk3,1)" id="chk3">
    	<tbody><tr>
    		<td><span style="color: #99CCFF;" title="Aftab duza"><input type="checkbox" name="chk3$0" id="chk3_0"><label for="chk3_0">Aftab duza</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Amin Islam"><input type="checkbox" name="chk3$1" id="chk3_1"><label for="chk3_1">Amin Islam</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Arifa katon"><input type="checkbox" name="chk3$2" id="chk3_2"><label for="chk3_2">Arifa katon</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Bappy das"><input type="checkbox" name="chk3$3" id="chk3_3"><label for="chk3_3">Bappy das</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="hasem amla"><input type="checkbox" checked="checked" name="chk3$4" id="chk3_4"><label for="chk3_4">hasem amla</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Katrina kaif"><input type="checkbox" name="chk3$5" id="chk3_5"><label for="chk3_5">Katrina kaif</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="masum khan"><input type="checkbox" name="chk3$6" id="chk3_6"><label for="chk3_6">masum khan</label></span></td>
    	</tr><tr>
    		<td><span style="color: #ff9aa4;" title="Rabiul Hossain"><input type="checkbox" name="chk3$7" id="chk3_7"><label for="chk3_7">Rabiul Hossain</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="Shahid Ullah"><input type="checkbox" name="chk3$8" id="chk3_8"><label for="chk3_8">Shahid Ullah</label></span></td>
    	</tr><tr>
    		<td><span style="color: #99CCFF;" title="sumon mia"><input type="checkbox" name="chk3$9" id="chk3_9"><label for="chk3_9">sumon mia</label></span></td>
    	</tr>
    </tbody></table></div></div></div></div></div></div>
                                                    </div>

    Please help me

    Thursday, July 30, 2015 1:22 PM

Answers

  • User-330204900 posted

    you will need to search reclusively see my Extension Method examples here:

    /// <summary>
    /// Get the control by searching recursively for it.
    /// </summary>
    /// <param name="Root">The control to start the search at.</param>
    /// <param name="Id">The ID of the control to find</param>
    /// <returns>The control the was found or NULL if not found</returns>
    public static Control FindControlRecursive(this Control Root, string Id)
    {
    	if (Root.ID == Id)
    		return Root;
    
    	foreach (Control Ctl in Root.Controls)
    	{
    		Control FoundCtl = FindControlRecursive(Ctl, Id);
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }
    
    /// <summary>
    /// Finds the control recursive.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="root">The root.</param>
    /// <returns></returns>
    public static T FindControlRecursive<T>(this Control root) where T : Control
    {
    	var control = root as T;
    	if (control != null)
    		return control;
    
    	foreach (Control Ctl in root.Controls)
    	{
    		T FoundCtl = Ctl.FindControlRecursive<T>();
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }
    
    /// <summary>
    /// Finds the control recursive.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="root">The root.</param>
    /// <param name="Id">The id.</param>
    /// <returns></returns>
    public static T FindControlRecursive<T>(this Control root, string Id) where T : Control
    {
    	var control = root as T;
    	if (control != null && root.ID == Id)
    		return control;
    
    	foreach (Control Ctl in root.Controls)
    	{
    		T FoundCtl = Ctl.FindControlRecursive<T>(Id);
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 31, 2015 7:48 AM

All replies

  • User1686483761 posted

    Hi, 2ramin

    it could be caused by the following reason:

    1. When you add controls dynamically, you must re-add them when the page posts back . It's because you are adding control dynamically to page and

    when you click on the button page get postback  and remove the dynamically added controls that why its not able to find checkbox control in button click

    event. For dynamic control check the post: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

    At the same time, you could refer to this link(http://forums.asp.net/t/1488479.aspx ) ,so that you could understand why Dynamicall created checkboxList shows null

    And i have a suggestion:

    To avoid this situation, you can use js to handle click events on the client side ,you could also  get the checkboxlist information ,

    and send the information to the server side by ajax, you could refer to http://stackoverflow.com/questions/9291126/array-to-send-with-jquery-post

    i hope it's helpful for you

    Best Regards

    cheng zhang

    Friday, July 31, 2015 2:26 AM
  • User-330204900 posted

    you will need to search reclusively see my Extension Method examples here:

    /// <summary>
    /// Get the control by searching recursively for it.
    /// </summary>
    /// <param name="Root">The control to start the search at.</param>
    /// <param name="Id">The ID of the control to find</param>
    /// <returns>The control the was found or NULL if not found</returns>
    public static Control FindControlRecursive(this Control Root, string Id)
    {
    	if (Root.ID == Id)
    		return Root;
    
    	foreach (Control Ctl in Root.Controls)
    	{
    		Control FoundCtl = FindControlRecursive(Ctl, Id);
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }
    
    /// <summary>
    /// Finds the control recursive.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="root">The root.</param>
    /// <returns></returns>
    public static T FindControlRecursive<T>(this Control root) where T : Control
    {
    	var control = root as T;
    	if (control != null)
    		return control;
    
    	foreach (Control Ctl in root.Controls)
    	{
    		T FoundCtl = Ctl.FindControlRecursive<T>();
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }
    
    /// <summary>
    /// Finds the control recursive.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="root">The root.</param>
    /// <param name="Id">The id.</param>
    /// <returns></returns>
    public static T FindControlRecursive<T>(this Control root, string Id) where T : Control
    {
    	var control = root as T;
    	if (control != null && root.ID == Id)
    		return control;
    
    	foreach (Control Ctl in root.Controls)
    	{
    		T FoundCtl = Ctl.FindControlRecursive<T>(Id);
    
    		if (FoundCtl != null)
    			return FoundCtl;
    	}
    	return null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 31, 2015 7:48 AM