4

I am trying to read from a dynamically created checkbox on button click. The problem is that once the checkbox is checked, further uncheck operation are not read properly on submit click.

EDIT: The checkbox is initially created on the selection of a radiobuttonlist by calling SetSelection as shown.

The code snippet is shown below, Any idea what could be the possible problem?

protected void Page_Load(object sender, EventArgs e)
{    
    if (this.IsPostBack)
    {
    ..
        GenerateDynamicUI();
    }
    ...
}     


private void GenerateDynamicUI(int selectedItem)
{
    ...
    TableCell cellCheckBox = new TableCell();
    CheckBox chkBox = new CheckBox();              
    chkBox.Text = "Consider all";
    chkBox.ID = "chkAll";
    cellCheckBox.Controls.Add(chkBox);

    TableRow chkRow = new TableRow();
    chkRow.Cells.Add(cellCheckBox);
    table.Rows.Add(chkRow);
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ...
    bool isChecked = ((CheckBox)table.FindControl("chkAll")).Checked;   

}

private void SetSelection()
{
    int selectedItem = int.Parse(radiobuttonList.SelectedItem.Value);           
    GenerateDynamicUI(selectedItem);
    pnlDynamic.Visible = true;            
}

protected void radiobuttonList_SelectedIndexChanged(object sender, EventArgs e)
{
     SetSelection();
}       
6
  • Does this CheckBox already present in your webform code or it will just created on Postback? Commented Jan 15, 2012 at 17:04
  • It's created dynamically, in his GenerateDynamicUI method. Commented Jan 15, 2012 at 17:07
  • I cant find any issue in this code its giving me correct value of checkbox each time. Can you elaborate your problematic scenario? Commented Jan 15, 2012 at 17:22
  • I second that, also is this the entire declaration of the CheckBox and its containing Controls? I'm wondering if the CheckBox is being modified at some stage? Commented Jan 15, 2012 at 17:38
  • Please see edited code. I am having a radiobutton list. Based on the selection, I am creating the checkbox dynamically using SetSelection method. SCENARIO: When the checkbox is not checked and when I do a submit, isChecked is giving false and when I check it and do submit, it is returning true (as expected). But when I uncheck it again, still it is returning true. Commented Jan 15, 2012 at 17:47

2 Answers 2

4

I've recreated your example and it works fine. I can only imagine there's something else in your code responsible for the unexpected behaviour.

Try using the Page_PreInit event rather than Page_Load to re-create/manipulate your dynamic controls:

protected void Page_PreInit(object sender, EventArgs e)
{
    // create controls here
    GenerateDynamicUI();
}

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

By 'not ready properly' I assume you mean it stays True and never returns False after the first time you've check it?

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

2 Comments

Yes, it stays True and never returns False after the first time I check it.
+1 for building dynamic controls in PreInit. That's where they belong
2

it looks like you declaring

bool isChecked = ((CheckBox)table.FindControl("chkAll")).Checked;

in the btnSubmit if so it would be reset to false every time the method is called. try declaring it out side. IE:

bool isChecked;
protected void btnSubmit_Click(object sender, EventArgs e)
{
    ...
    isChecked = ((CheckBox)table.FindControl("chkAll")).Checked;   

}

1 Comment

This won't matter, it's a local var. Furthermore it's written to each time.

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.