Click here to Skip to main content
15,894,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Dynamic generate checkbox control.
In Process control function my checkbox is always false (chk.Checked).
Why my Checkbox check state is loss.
Please give me proper solution of this.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        PopulateLeftSearchPane();

}

PopulateLeftSearchPane()
{
    UpdatePanel myupdpnl = new UpdatePanel();
               myupdpnl.ID = "upnl" + cn.AspectID + "_" + cn.TraitID;

                CheckBox chk = new CheckBox();
                chk.ID = "chktrait" + cn.AspectID + "_" + cn.TraitID;
                //string uniqueID = System.Guid.NewGuid().ToString().Substring(0, 5);
                //chk.ID = "chktrait";
                //chk.ID = "chktrait" + cn.AspectID + "_" + cn.TraitID + uniqueID;
                chk.Text = cn.ValueName;
                chk.Attributes.Add("AspectID", cn.AspectID.ToString());
                chk.Attributes.Add("TraitID", cn.TraitID.ToString());

                chk.AutoPostBack = false;
                //CheckBox chk = (CheckBox)ctrl;
                //  chk.CheckedChanged += new EventHandler(CheckBox_CheckChanged);
                // chk.CheckedChanged += new EventHandler(this.CheckBox_Click);
                chk.Attributes.Add("onchange", "Javascript:return CallSearchResults('" + chk.ID + "','" + cn.AspectID + "','" + cn.TraitID + "','" + cn.ValueName + "'" + ");");
                myupdpnl.ContentTemplateContainer.Controls.Add(chk);
              
                //myupdpnl.ContentTemplateContainer.Controls.Add(chk);

                divaccordioninner.Controls.Add(myupdpnl);
}




protected void btnSearchResult_Click(object sender, EventArgs e)
    {

        ProcessControls(divLeftpane,false);
     

    }


C#
private void ProcessControls(Control ctrlContainer, bool Isall)
    {
        foreach (Control ctrl in ctrlContainer.Controls)
        {
            if (ctrl.GetType() == typeof(TextBox))
            {
                // Do whatever to the TextBox
            }

            else if (ctrl.GetType() == typeof(CheckBox))
            {
                // Do whatever to the TextBox
                //Response.Write(ctrl.UniqueID);
                CheckBox chk = (CheckBox)ctrl;
                if (Isall == false)
                {
                    if (chk.Checked)
                    {
                        Int64 intAspectID = Convert.ToInt64(chk.Attributes["AspectID"]);
                        Int64 intTraitID = Convert.ToInt64(chk.Attributes["TraitID"]);
                        SearchResult(intAspectID, intTraitID);
                    }
                }
                else
                {
                    Int64 intAspectID = Convert.ToInt64(chk.Attributes["AspectID"]);
                    Int64 intTraitID = Convert.ToInt64(chk.Attributes["TraitID"]);
                    SearchResult(intAspectID, intTraitID);
                }
            }
            else if (ctrl.GetType() == typeof(RadioButton))
            {
                // Do whatever to the TextBox
            }
            else if (ctrl.GetType() == typeof(Image))
            {
                // Do whatever to the TextBox
            }

            if (ctrl.HasControls())
            {
                if (Isall == false)
                {
                    ProcessControls(ctrl, false);
                }
                else
                {
                    ProcessControls(ctrl, true);
                }
            }
        }


    }
Posted
Comments
johannesnestler 25-Jul-14 5:02am    
Your code doesn't Show the interesting bits, but I would suspect you are recreating a new Checkbox when you call CheckBox chk = new CheckBox(); did you mean to initialize a member variable? So conculstion - you have a Logical bug. have a look where you create your Checkbox, and all possible code-paths to influence the Checkbox- better spend some time Debugging - I think you will find the Problem faster than through QA...
ZurdoDev 25-Jul-14 7:18am    
Move dynamic controls into Page OnInit event instead.

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