Validate required drop down lists.

Agosto 30th, 2007

Description: Use a CompareValidator to ensure that the user has made a valid selection from a drop down list.

Link: http://www.codekeep.net/snippets/56f51d03-b35d-4e87-8033-4741d4e4985b.aspx

-------------------------------------
Here are the drop down list and validator declarations.
The example here is a list of products.
-------------------------------------
<asp:DropDownList*Selection Required" ErrorMessage="You must select a Product"></asp:CompareValidator>

-------------------------------------
Here is the C# code to bind the list and add a default entry.
Note that the value of the default entry is -1.
-------------------------------------
lstProducts.DataSource = GetProducts();
lstProducts.DataBind();
insertDefaultItem(lstProducts);

private void insertDefaultItem(DropDownList ddl)
{
	ddl.Items.Insert(0, new ListItem("<Select One>", "-1"));
}

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.