Databound下拉列表 - 初始值

Databound下拉列表 - 初始值

问题描述:

如何在ASP.NET中设置数据绑定下拉列表的初始值?例如,我想要的值,但显示的第一个值应该是 - 选择一个---,一个空值。Databound下拉列表 - 初始值

我想你想要做的是这样的:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"> 
    <asp:ListItem Text="--Select One--" Value="" /> 
</asp:DropDownList> 

确保‘AppendDataBoundItems’设置为,否则你将清除“ - 当你绑定你的数据时选择一个'列表项。

如果你有下拉列表设置为真正你必须还“的CausesValidation”属性设置为然后用“的RequiredFieldValidator”以确保“的“的AutoPostBack”属性 - 选择一个 - '选项不会导致回发。

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator> 
+0

谢谢,这正是我所寻找的。 :-) +1 – 2009-10-05 14:05:12

+0

这一个非常有帮助。 – Denis 2014-02-11 20:29:12

添加一个项目并将它的“Selected”属性设置为true,您可能希望将“appenddatabounditems”属性设置为true,以便在数据绑定时不会删除您的初始值。

如果您正在讨论设置数据绑定项目中的初始值,然后挂钩您的ondatabound事件并设置要选择哪个索引= true,则您需要将其封装在“if not page.isPostBack”中。 ......”虽然

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 

我所做的是设置下拉列表的文本属性后,我databind它。事情是这样的:

protected void LoadPersonComboBox() 
    { 
     var p = new PeopleBLL(); 

     rcmbboxEditPerson.DataSource = p.GetPeople(); 
     rcmbboxEditPerson.DataBind(); 
     rcmbboxEditPerson.Text = "Please select an existing person to edit..."; 
    } 

这使得该下拉菜单的初始值明显显现出来,但实际上没有下降的一部分下来,也不是一个选择。

我知道这已经有一个选择的答案 - 但我想折腾我的两分钱。 我有一个数据绑定的下拉列表:

<asp:DropDownList 
    id="country" 
    runat="server" 
    CssClass="selectOne" 
    DataSourceID="country_code" 
    DataTextField="Name" 
    DataValueField="CountryCode_PK" 
></asp:DropDownList> 
<asp:SqlDataSource 
    id="country_code" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
    SelectCommand="SELECT CountryCode_PK, CountryCode_PK + ' - ' + Name AS N'Name' FROM TBL_Country ORDER BY CountryCode_PK" 
></asp:SqlDataSource> 

在代码隐藏,我有这个 - (默认情况下选择美国):

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

该页面最初加载基于“美国的价值而不是试图担心一个selectedIndex(如果另一个项目被添加到数据表中 - 我不想重新编码)

我知道这是旧的,但这些想法的组合导致一个非常优雅的解决方案:

保留DropDownList的所有默认属性设置(AppendDataBoundItems = false,Items为空)。然后处理数据绑定事件是这样的:

protected void dropdown_DataBound(object sender, EventArgs e) 
{ 
    DropDownList list = sender as DropDownList; 
    if (list != null) 
     list.Items.Insert(0, "--Select One--"); 
} 

上锦上添花的是,这一个处理程序可以通过任意数量的DropDownList对象共享,甚至可以放入一个通用工具库的所有项目。

要选择从下拉菜单中的值使用这样的指标:

如果我们有

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"></asp:DropDownList> 

你可以使用:

DropDownList1.Items[DropDownList1.SelectedIndex].Value 

将返回值的选定的索引。在这种情况下

dropdownlist.Items.Insert(0, new Listitem("--Select One--", "0"); 

喜的朋友,你可以使用

AppendDataBound="true" 

并在此之后使用列表项。 用于例如: -

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"> 
    <asp:ListItem Text="--Select One--" Value="" /> 
</asp:DropDownList> 

但在这个问题后第二次选择数据与旧数据追加。