Databound drop down list - initial value

后端 未结 8 1879
小蘑菇
小蘑菇 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:58

    What I do is set the text property of the drop down list AFTER I databind it. Something like this:

       protected void LoadPersonComboBox()
        {
            var p = new PeopleBLL();
    
            rcmbboxEditPerson.DataSource = p.GetPeople();
            rcmbboxEditPerson.DataBind();
            rcmbboxEditPerson.Text = "Please select an existing person to edit...";
        }
    

    This makes the initial visible value of this dropdown show up, but not actually be a part of the drop down, nor is it a selectable.

    0 讨论(0)
  • 2020-12-24 04:02

    Add an item and set its "Selected" property to true, you will probably want to set "appenddatabounditems" property to true also so your initial value isn't deleted when databound.

    If you are talking about setting an initial value that is in your databound items then hook into your ondatabound event and set which index you want to selected=true you will want to wrap it in "if not page.isPostBack then ...." though

    Protected Sub DepartmentDropDownList_DataBound(ByVal sender As Object, ByVal e As    System.EventArgs) Handles DepartmentDropDownList.DataBound
        If Not Page.IsPostBack Then
            DepartmentDropDownList.SelectedValue = "somevalue"
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题