0

In one of my web page, there are 12 checkbox controls in a div tag. I want to make sure the user check at least one checkbox in a div after they submit the form.in one of my asp.net web page. Any idea? I mean server side. I have done client side, but as you know, no one could guarantee all client browser enable javascript.

DShultz
  • 4,381
  • 3
  • 30
  • 46
Steven Zack
  • 4,984
  • 19
  • 57
  • 88
  • 2
    Do you want to check client or server side? what have you tried? Post your code! – danyolgiax Jun 20 '11 at 21:33
  • 1
    If javascript is disabled it is impossible to verify at least one checkbox is checked before submit is clicked. You must use javascript for this. – hova Jun 20 '11 at 21:38
  • Not only that, but iirc you need javascript do do post backs due to the way asp.net does postbacks (through the __doPostback function). – SRM Jun 20 '11 at 21:40

6 Answers6

1

Since you're using ASP.Net, you may want to consider using the <asp:CheckboxList /> control, and add an <asp:CustomValidator> plus validation functions that ensure one checkbox was checked.

DShultz
  • 4,381
  • 3
  • 30
  • 46
  • And the custom validator supports both server and client-side functions. – DShultz Jun 20 '11 at 21:40
  • You think so? As I know, if browser disables javascript, that validatior won't work. – Steven Zack Jun 20 '11 at 21:44
  • You will want to create a server validation function for your CustomValidator which ALWAYS executes:(OnServerValidate=ServerValidationFunction"). You can also specify the ClientValidationFunction="clientFunctionName" as a nicety for all JavaScript enabled users. You don't want to just rely on client validation, but it's good for saving your js-enabled users an unnecessary postback. – DShultz Jun 20 '11 at 23:26
0

Can you use JQuery? If so, check this out:

Get a list of checked checkboxes in a div using jQuery

Community
  • 1
  • 1
mtazva
  • 1,005
  • 9
  • 13
0

Does it have to be is C#? Sounds a lot simplier if you just did it in javascript.

Limey
  • 2,642
  • 6
  • 37
  • 62
0

You want to look at the OnClientClick property of the asp:CheckBox control.

Here is an example:

<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />

In your javascript code (called by the handler) you can loop through all children of the div, looking for checkbox type inputs. jQuery is best for that. You could use something like this:

function countChecked() {
  return $("input:checked").length;
}

That function above will return the number of checked checkboxes. After that it's trivial to validate your form. Just remember to return false from the handler called by OnClientClick to avoid a postback (in the event your form doesn't validate).

I just realized you edited your question while I was typing an answer. The above is a client side solution only.

SRM
  • 1,377
  • 1
  • 10
  • 17
  • I want to know how to verify in server side. – Steven Zack Jun 20 '11 at 21:39
  • @Steven Zack Your question was a little misleading then. Also, IIRC ASP.Net won't run without javascript enabled anyway (no way to do postbacks). Not only that, but MS itself is now advocating the use of jQuery whenever possible to do these kinds of things. Frankly, the only people not using javascript are those born on the moon or if you are using some hardened infrastructure like SIPRANet (CnC for the military). – SRM Jun 20 '11 at 21:45
  • well said. I'm a big fan of jQuery, but my supervisor requires my application could be accessed by both enable javascript and disable javascript browsers. So I have to write server side validation code as well. – Steven Zack Jun 20 '11 at 21:48
  • actually, I have your code in front page, but if js is disabled, you know, the beautiful jQuery won't work. – Steven Zack Jun 20 '11 at 21:50
  • @Steven Yep, that's the case, but can you even run asp.net without js enabled? IIRC everything besides button uses the javascript function __doPostback to do it's magic. Is your supervisor one of those guys that says "well, 1% of the population still uses ie6 so we have to support that also"? ;) – SRM Jun 21 '11 at 00:53
0

If you just want a server side check for this and dont mind autopostback, give this a try. Just set an event handler for the SelectedIndexChanged event and check to see if an option is selected there. You can then display an error of your choice.

Here is the checkboxlist code:

    <asp:CheckBoxList ID="chkBxList" runat="server" AutoPostBack="true" 
        onselectedindexchanged="chkBxList_SelectedIndexChanged">
        <asp:ListItem>option1</asp:ListItem>
        <asp:ListItem>option2</asp:ListItem>
        <asp:ListItem>option3</asp:ListItem>
    </asp:CheckBoxList>
    <asp:Label id="lblError" runat="server"></asp:Label>

Codebehind:

protected void chkBxList_SelectedIndexChanged(object sender, EventArgs e)
{
    bool oneSelected = false;
    foreach (ListItem item in chkBxList.Items)
    {
        if (item.Selected)
            oneSelected = true;
    }

    if (!oneSelected)
        lblError.Text = "Please select an option from the checkbox list.";
    else
        lblError.Text = "At least one checkbox is selected";
}

Even if the client disables JS this will still make sure they choose at least one.

tubsy3
  • 1
0

While I agree with what others said about the validators, this code does what you want:

int i = 0;
foreach (Control ctl in myForm.FindControl("myDiv").Controls)
{
    if (ctl is CheckBox)
    {
        if (((CheckBox)ctl).Checked)
            i++;
    }
}
Teletha
  • 603
  • 1
  • 11
  • 21