Thursday, March 22, 2012

Setting the default value of a dropdownlist

I have a dropdownlist I am populating on the Page_Load. How do I set the default value to one of the items in the Dropdownlist when the page first appears.After you have called Databind on your dropdownlist use:

DropDownList.Items.Insert(0, "Please Select Item")

Simon
Why would you want to do that. If the user submitted the form by mistake they may submit a value they didnt intend to.

Why not set the inital dropdown list value to a non selectable item. This way they have to choose something. You could then also set a validator against it so that the page will not submit until the user selects a valid value.
Firstly add DropDownList.SelectedIndex = 0 after my first line.

Sorry misread your original post, but midi 25 is right probably better to leave it blank or use my snipper then check for null or 0 using a validator.

If you really wanted to select an item from your databound items use


DropDownList.Items.FindByValue("YourValueYouWantSelected").Selected = True

Simon
Hello,

How can you check for null or 0 using a validator?

Also, my users table has a categories field. When they first choose the category from the drop down all is well.
However, if they come back to change their details - How can I have the dropdown list default to their selected category?

Thanks
JB

0 comments:

Post a Comment