Hello, I want to validate that at least one of the checkbox has been checked from the CheckBoxList Control on my webform page. There are four items in the checkboxlist. Now, when I select either one from the first three checkbox items, an error would appears
says " at least one box must be checked", but if I selected all four checkbox items or only the fourth one. The message would display as "Ok". I don't know why when I selected on either one of the first three items an error would encountered, could anyone
help me? Thanks! code in c#
using System;
using System.Collections;
--
namespace TESTING1
{
public class checkboxlist : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBoxList pickName;
bool valChkBoxList;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Label1.Text="";
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
validate_chkboxlist();
if (valChkBoxList == true)
{
for (int i=0; i < CheckBoxList1.Items.Count ; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
Label1.Text ="ok";
}
else
{
Label1.Text ="You must select at least one CheckBox!";
}
}
}
}
private void validate_chkboxlist()
{
for (int i=0; i i++)
{
if(CheckBoxList1.Items[i].Selected)
{valChkBoxList = true;}
}
}
}
}
I am not sure about the red colored code, should it be something like in the VB.net CheckBoxList1.Items.Count-1; But I don't know how to do it in C#.
Thanks fancyketsup, Now, when I checked either one of the checkboxes it returns the Ok message. However, when I hit the submit button before select any checkboxes, I don't get the "you must select at least one box" message. Could you check for me, did I do
anything wrong again? Also, is there a way when I deselect the box and the Ok message would disappear?
Microsoft designed its validators to let you communicate with users in a consistent fashion. For example, you can build your logic into a CustomValidator and have it work like any other validator. However, it will not have the client-side validation code, unless
you also create that. If you want a premade validator that requires at least one checkbox marked in a CheckBoxList, my product
Professional Validation And More offers a solution. Its RequiredListValidator, one of 22 validators it offers, does this.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Thank you so much! Now, I am having some other problem. I try to add these code to my real application. On my Approve.aspx page, I have a approve button. The code inside the Approve_Click, I have a server.transfer("next.aspx") command AND an update function(e.g.:
update();) that contains connection to a database and Update command. I have to command out all the update command and server.transfer command to let the validation to work. Otherwise, it didn't really verify if the boxes is selected or not. Do you think if
there is a way to make this work? Thanks for your patience to help me out!
You can do client side validation pretty easy but to do server side validation takes more than a few lines of code. If all you want is to run a database update based on a condition, server side validation may be overkill. Keep in mind that client side validation
will not work 100% of the time depends on the browser. Besides if its real important you probably want validation done on the server anyway.
for information about server side validators here is a link.... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconvalidatorcontrolsamples.asp
Answering a question increases your knowledge asking a question shows your Intelligence!
aspshare
Member
200 Points
40 Posts
CheckBoxList Validation not working properly.
Mar 15, 2004 08:54 PM|LINK
using System; using System.Collections; -- namespace TESTING1 { public class checkboxlist : System.Web.UI.Page { protected System.Web.UI.WebControls.CheckBoxList pickName; bool valChkBoxList; private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { Label1.Text=""; } } private void Button1_Click(object sender, System.EventArgs e) { validate_chkboxlist(); if (valChkBoxList == true) { for (int i=0; i < CheckBoxList1.Items.Count ; i++) { if (CheckBoxList1.Items[i].Selected) { Label1.Text ="ok"; } else { Label1.Text ="You must select at least one CheckBox!"; } } } } private void validate_chkboxlist() { for (int i=0; i i++) { if(CheckBoxList1.Items[i].Selected) {valChkBoxList = true;} } } } }I am not sure about the red colored code, should it be something like in the VB.net CheckBoxList1.Items.Count-1; But I don't know how to do it in C#.FancyKetsup
Participant
1901 Points
391 Posts
Re: CheckBoxList Validation not working properly.
Mar 15, 2004 10:14 PM|LINK
private void Button1_Click(object sender, System.EventArgs e) { validate_chkboxlist(); if (valChkBoxList == true) { for (int i=0; i < CheckBoxList1.Items.Count ; i++) { if (CheckBoxList1.Items[i].Selected) { Label1.Text ="ok"; break; } else { Label1.Text ="You must select at least one CheckBox!"; } } } } private void validate_chkboxlist() { for (int i=0; i<CheckBoxList1.Items.Count; i++) { if(CheckBoxList1.Items[i].Selected) {valChkBoxList = true;} break; } }aspshare
Member
200 Points
40 Posts
Re: CheckBoxList Validation not working properly.
Mar 15, 2004 11:03 PM|LINK
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: CheckBoxList Validation not working properly.
Mar 15, 2004 11:29 PM|LINK
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
FancyKetsup
Participant
1901 Points
391 Posts
Re: CheckBoxList Validation not working properly.
Mar 16, 2004 12:21 AM|LINK
private void Button1_Click(object sender, System.EventArgs e) { validate_chkboxlist(); } private void validate_chkboxlist() { for (int i=0; i<CheckBoxList1.Items.Count; i++) { if(CheckBoxList1.Items[i].Selected) { valChkBoxList = true; Label1.Text ="ok"; break; } else { Label1.Text ="You must select at least one CheckBox!"; } } }or even shorter just this would work...private void Button1_Click(object sender, System.EventArgs e) { for (int i=0; i
aspshare
Member
200 Points
40 Posts
Re: CheckBoxList Validation not working properly.
Mar 16, 2004 01:50 AM|LINK
FancyKetsup
Participant
1901 Points
391 Posts
Re: CheckBoxList Validation not working properly.
Mar 16, 2004 04:54 PM|LINK
private void Button1_Click(object sender, System.EventArgs e) { for (int i=0; i<CheckBoxList1.Items.Count; i++) { if(CheckBoxList1.Items[i].Selected) { doMyDataBaseMethod(); break; } } }for information about server side validators here is a link.... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconvalidatorcontrolsamples.asp