Databound drop down list - initial value

后端 未结 8 1899
小蘑菇
小蘑菇 2020-12-24 03:21

How do I set the initial value of a databound drop down list in ASP.NET?

For instance, I want the values, but the first value to display should be -- Select One ---

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 03:55

    I know this already has a chosen answer - but I wanted to toss in my two cents. I have a databound dropdown list:

    
    
    

    In the codebehind, I have this - (which selects United States by default):

    if (this.IsPostBack)
    {
      //handle posted data
    }
    else 
    {
      country.SelectedValue = "US";
    }
    

    The page initially loads based on the 'US' value rather than trying to worry about a selectedIndex (what if another item is added into the data table - I don't want to have to re-code)

提交回复
热议问题