Asp.net 数据绑定下拉列表-初始值
如何在ASP.NET中设置数据绑定下拉列表的初始值Asp.net 数据绑定下拉列表-初始值,asp.net,vb.net,drop-down-menu,Asp.net,Vb.net,Drop Down Menu,如何在ASP.NET中设置数据绑定下拉列表的初始值 例如,我需要这些值,但要显示的第一个值应该是-Select One-,并带有空值。添加一个项并将其所选属性设置为true,您可能还需要将appenddatabounditems属性设置为true,以便在数据绑定时不会删除初始值 如果您正在谈论设置数据绑定项中的初始值,则将其挂接到ondatabound事件中,并设置要选择的索引=true如果不是page.isPostBack,则您将希望将其包装在其中。。。。虽然 Protected Sub De
例如,我需要这些值,但要显示的第一个值应该是-Select One-,并带有空值。添加一个项并将其所选属性设置为true,您可能还需要将appenddatabounditems属性设置为true,以便在数据绑定时不会删除初始值 如果您正在谈论设置数据绑定项中的初始值,则将其挂接到ondatabound事件中,并设置要选择的索引=true如果不是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
我想你要做的是:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
确保“AppendDataBoundItems”设置为true,否则在绑定数据时将清除“-Select One-”列表项
如果下拉列表的“AutoPostBack”属性设置为true,则还必须将“CausesValidation”属性设置为true,然后使用“RequiredFieldValidator”确保“-Select One-”选项不会导致回发
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
我要做的是在对下拉列表进行数据绑定后设置其text属性。大概是这样的:
protected void LoadPersonComboBox()
{
var p = new PeopleBLL();
rcmbboxEditPerson.DataSource = p.GetPeople();
rcmbboxEditPerson.DataBind();
rcmbboxEditPerson.Text = "Please select an existing person to edit...";
}
protected void dropdown_DataBound(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
if (list != null)
list.Items.Insert(0, "--Select One--");
}
这会显示此下拉列表的初始可见值,但实际上它不是下拉列表的一部分,也不是可选的。我知道这已经有了一个选定的答案,但我想投入2美分。 我有一个数据绑定的下拉列表:
<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>
页面最初是基于“US”值加载的,而不是试图担心一个selectedIndex,如果在数据表中添加了另一项会发生什么-我不想重新编码我知道这很旧,但这些想法的结合会带来一个非常优雅的解决方案: 保留DropDownList AppendDataBoundItems=false、Items为空的所有默认属性设置。然后按如下方式处理数据绑定事件:
protected void LoadPersonComboBox()
{
var p = new PeopleBLL();
rcmbboxEditPerson.DataSource = p.GetPeople();
rcmbboxEditPerson.DataBind();
rcmbboxEditPerson.Text = "Please select an existing person to edit...";
}
protected void dropdown_DataBound(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
if (list != null)
list.Items.Insert(0, "--Select One--");
}
锦上添花的是,这个处理程序可以被任意数量的DropDownList对象共享,甚至可以放入所有项目的通用实用程序库中。要从下拉列表中选择一个值,请使用如下索引:
protected void LoadPersonComboBox()
{
var p = new PeopleBLL();
rcmbboxEditPerson.DataSource = p.GetPeople();
rcmbboxEditPerson.DataBind();
rcmbboxEditPerson.Text = "Please select an existing person to edit...";
}
protected void dropdown_DataBound(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
if (list != null)
list.Items.Insert(0, "--Select One--");
}
如果我们有
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
这将返回所选索引的值。您好,在这种情况下,您可以使用
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>
但问题是,在第二次使用旧数据附加select数据之后。在哪里调用此方法?@iQue您可以将其设置为DropDownList的OnDataBound事件。我认为这比公认的答案要好得多,因为当数据源发生变化并且我们重新进行数据绑定时,就没有问题了;有了公认的答案,重新数据绑定只会将元素添加到现有列表中,这些元素可能是您想要的,但似乎更可能不是您想要的。