开发者

get dynamic checkbox value on button click

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); 
 开发者_StackOverflow                           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);
                }
            }
        }
    }


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


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");
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜