1

i have a page where i create 2 checkboxes dynamically.

 TableRow tr = new TableRow();
                    for (int i = 0; i < 2; i++)
                    {
                        TableCell Tc = new TableCell();


                            Tc.Attributes["style"] = "line-height: 30px; text-align: left";
                            Tc.Attributes["width"] = "50%";
                            Tc.Style.Add("padding-left", "5px");
                            //Checkboxes on left along with labels
                            CheckBox checkBoxCtrl = new CheckBox();
                            checkBoxCtrl.ID = "checkBoxCtrl" + i;
                            Tc.Controls.Add(checkBoxCtrl); 
                            tr.Cells.Add(Tc);                           
                    }

once they are created in the page load event i have a Ok_button click event which requires to check if the checkbox is checked or not.

 protected void Update2_Click(object sender, EventArgs e)
        {


         if(checkBoxCtrl.checked)
          //here i wont be able to get the value 
          // i get the error the name checkBoxCtrl does not exist..
          {
             response.write("true");
           }

        }

but how do i do the check in this case.

thanks

Answer:

this is what needs to be done to get the checkbox values

 protected void Update1_Click(object sender, EventArgs e)
    {
        for(int i = 0; i < ControlPropList.Count; i++)
        {              
            CheckBox chkTest = (CheckBox)xxx.FindControl("checkBoxCtrl" + i);
            {
                if (chkTest.Checked)
                {
                    Global.logger.Info("Checkbox True = " + chkTest.ID);
                }
                else
                {
                    Global.logger.Info("Checkbox False = " + chkTest.ID);
                }
            }
        }
    }
1
  • You need to save the checkboxes in Session for that user and add the checkboxes on every page load and make sure you keep codebehind references to them. It can also be done with javascript, so it's executed on the client, if thats what you want. Commented Aug 10, 2011 at 14:07

3 Answers 3

1

This should work fine as long as you add the checkboxes to your page in the Page_PreInit method. If you add them after that (Page_Load for example), their values will not be maintained.

Read about the asp.net page lifecycle here:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

i kno this works it creates a checkbox.. but after the page is created suppose i check the checkbox and click an OK button how do i check if the find the checkbox with the dynamic checkbox id and get the value from it?
Exactly how you're doing it now. What I'm saying is that although you will still see the checkbox and be able to click it if you create it on Page_Load, you won't be able to check the value of it correctly unless you add it to the page in Page_PreInit.
will i need to create in page_init and page_load or only page_init will do? what if pageload happens/.. thanks
1

Consider storing the dynamic checkbox in a local member:

    private CheckBox _myCustomCheckbox = new CheckBox();

    protected override void OnInit(EventArgs e)
    {
        TableRow tr = new TableRow();
        for (int i = 0; i < 2; i++)
        {
            TableCell Tc = new TableCell();

            if (i == 0)
            {
                Tc.Attributes["style"] = "line-height: 30px; text-align: left";
                Tc.Attributes["width"] = "50%";
                Tc.Style.Add("padding-left", "5px");
                //Checkboxes on left along with labels

                _myCustomCheckbox.ID = "checkBoxCtrl" + j;
                Tc.Controls.Add(_myCustomCheckbox);
                tr.Cells.Add(Tc);
            }
        }

        // the row needs added to a page control so that the child control states can be loaded 
        SomeTableOnThePage.Controls.Add(tr);

        base.OnInit(e);
    }

    protected void Update2_Click(object sender, EventArgs e)
    {
        if(_myCustomCheckbox.Checked)
        {
            response.write("true");
        }
    }

2 Comments

thanks this is great..what if there are multiple checkboxes how do i check then..??
please can you see my edit as i will have 2 checkboxes with id's checkBoxCtrl0 and checkBoxCtrl1 and i need to check both of them... if i solve this i am good to go
0

May not be quite what you want, but I had a similar issue, I have a dynamically generated table in ASP.NET page, with dynamically generated CheckBoxes in one column. I have created the data for the table from a collection, and then as the dynamic CB's are created I give them an ID and store them in a second collection, such as an array of CB's.

So when I need to find the Checked value I simply iterate through the collection, and I can find the ones that are Checked.

Also as they were created simultaneously with the data in the dynamic table I was able to easily tie the table data row to the Checkbox value.

This obviously assumes that the dynamic table and CB's were created using some kind of looping.

This may not be the best solution but works for my current needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.