Tuesday, July 28, 2009

Setting a default value for a drop down list in .Net c#?

I am populating my drop down list from a database table, but I need the default to be the 2nd row from the table, as opposed to the first row. I cannot alter the table, so I am not really sure how to go about this.





EG:


TABLE


1 | John


2 | Steve


3 | Dylan





My dropdownlist (ddlNames) is populated in the above order. I need to have Steve as my defualt value.





Thanks in advance.

Setting a default value for a drop down list in .Net c#?
This is VB.NET so you might have to play with syntax but try this:





ddlNames.SelectedIndex = ddlNames.Items.IndexOf (ddlNames.Items.FindByText ("Steve"))





You can use a variable instead of hardcoding "Steve" ..close the extra spaces I used to make it fit here





hope that helps


No comments:

Post a Comment