0
votes

I Have a problem with two checkBoxes!

private CheckBox rdYes;
private CheckBox rdNo;

            rdYes = new CheckBox();
            rdYes.Text = "Yes";
            rdYes.Checked = false;
            rdNo = new CheckBox();
            rdNo.Text = "No";
            rdNo.Checked = false;

Error:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation

I instantiating the checkboxes in :

    protected override void CreateChildControls()
    {
            this.Controls.Add(rdYes);
            this.Controls.Add(rdNo);
     }

And I need to check one CheckBox.

enter image description here

2
Where are you actually instantiating the checkboxes? - Drew Kennedy
I have exam with CheckBox, because radioButton have AutoPostBack and to check only one, the page is refreshing... - Gohyu
CreateChildControls should also call the base method. - Alexander
I need a solution for this problem or not refreshing the page... - Gohyu
On check of check box , is refreshing right? - Jaydip Jadhav

2 Answers

1
votes

Try this

   rdYes = new CheckBox();
   rdYes.ID = "chkYes";
   rdYes.Text = "Yes";
   rdYes.Checked = false;
   rdYes.AutoPostBack=false;
   rdYes.Attributes.Add("onclick","ToggleCheckboxes();return false") 
   rdNo = new CheckBox();
   rdNo.ID = "chkNo";
   rdNo.Text = "No";
   rdNo.Checked = false;
   rdNo.AutoPostBack=false; 
   rdNo.Attributes.Add("onclick","ToggleCheckboxes();return false")

Add Javascript function to your page

function CheckCheckboxes() {

    if(document.getElementid('<%=chkYes.ClientID%>').checked==true)
    {
       document.getElementid('<%=chkNo.ClientID%>').checked=false;
    }
    else if(document.getElementid('<%=chkNo.ClientID%>').checked==true)
    {
      document.getElementid('<%=chkYes.ClientID%>').checked=false;
    }

 }
0
votes

And I need to check only one CheckBox...

If you need to check only one checkbox then you can better try to use Radio Button.

However if you want to make sure that only one checkbox is selected then try like this:

CheckBox prevChecked;
private void clickCheckBox(object sender, EventArgs e) 
{
   CheckBox myCheckBox = sender as CheckBox;
   if(myCheckBox != prevChecked && prevChecked!=null) prevChecked.Checked = false;
   {
       prevChecked = myCheckBox.Checked ? myCheckBox : null;
   }
}

and if you want that the page should not be refreshed then you can try to set AutoPostBack="false" for your Checkbox property.

rdYes = new CheckBox();
rdYes.Text = "Yes";
rdYes.Checked = false;
rdYes.AutoPostBack=false;
rdNo = new CheckBox();
rdNo.Text = "No";
rdNo.Checked = false;
rdNo.AutoPostBack=false;