Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Performing basic required validation on a CheckListBox

This is probably already known to 95% of all .NET developers, but I didn't know that you couldn't use a RequiredValidator web control with a CheckBoxList. So here's the approach I took, with thanks to a couple of quick google finds here and here.

A UserControl containing a CheckBoxList called cblServices contains the validation method and writes the error message if needed.

public bool CheckBoxListIsValid()
{
 bool blnValid = false;
 for(int i=0; i < cblServices.Items.Count; i++)
 {
  if (cblServices.Items[i].Selected)
   blnValid = true;
 }
 return blnValid;
}

public void WriteValidationError()
{
 lblInvalidCheckboxlist.Text = "Select Service(s)";
}

The submit button is located in the parent page, which performs the testing from the above usercontrol methods before proceeding onto the next process

if (!uc_mycontrol.CheckBoxListIsValid())
 uc_mycontrol.WriteValidationError();
else
{ .....


 

Comments (2) | Post RSS RSS comment feed

Posted on 11/10/2004 5:42:00 PM by Dave Burke
Categories: .NET
Tags:

Related posts

Comments (2) -

11/11/2004 12:41:00 PM Permalink

Good tip.  I'm sure I'll use it.

Erik |

11/11/2004 6:23:00 PM Permalink

Erik,  Well, its a low-tech approach but seems to work pretty well.  Thanks for your comments!

Dave Burke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke