如何添加默认的"选取[选择这个ASP.NET DropDownList控件? [英] How to add a default "Select" option to this ASP.NET DropDownList control?
问题描述
我是一个新的ASP.NET开发人员,我想了解LINQ到实体。我试图与LINQ的声明一个DropDownList绑定检索在状态实体状态列表。一切工作正常。不过,我现在尝试添加,选择选项DropDownList的,但它不会和我一起工作。
你能告诉我如何解决这个
的 ASP.NET代码:的
< ASP:DropDownList的ID =DropDownList1=服务器的AutoPostBack =真
OnSelectedIndexChanged =DropDownList1_SelectedIndexChanged >
的代码隐藏:的
保护无效的Page_Load(对象发件人,EventArgs五)
{
如果(!的IsPostBack)
{
DropDownList1.Items 。新增(新的ListItem(选择,0,真));
bindStatusDropDownList();
}
}
私人无效bindStatusDropDownList()
{
状态状态=新状态();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField =ID;
DropDownList1.DataTextField =说明;
DropDownList1.DataBind();
}
的 更新:的
我也试过在标记设置DropDownList的做,但它并没有跟我
<$ p工作过$ p>
< ASP:DropDownList的ID =DropDownList1=服务器的AutoPostBack =真
OnSelectedIndexChanged =DropDownList1_SelectedIndexChanged>
< ASP:列表项选择=真值=0文本=选择>< / ASP:ListItem的>
< / ASP:DropDownList的>
它不工作的原因是因为您要添加一个项目列表,然后重写整个列表用新的数据源
这将清除并重新填充您的列表,失去了先手动添加的项目。
所以,你需要做的反向是这样的:
状态状态=新状态();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField =ID;
DropDownList1.DataTextField =说明;
DropDownList1.DataBind();
//添加您的第一个项目
DropDownList1.Items.Insert(0,选择);
I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. I am trying to bind a DropDownList with the Linq statement for retrieving the list of status in the Status Entity. Everything is working fine. However, I am trying now to add "Select" option to the DropDownList but It doesn't work with me. Could you please tell me how to fix this?
ASP.NET Code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Select", "0", true));
bindStatusDropDownList();
}
}
private void bindStatusDropDownList()
{
Status status = new Status();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();
}
UPDATE:
I also tried to do in the markup set of the DropDownList but it did not work too with me
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem>
</asp:DropDownList>
The reason it is not working is because you are adding an item to the list and then overriding the whole list with a new DataSource
which will clear and re-populate your list, losing the first manually added item.
So, you need to do this in reverse like this:
Status status = new Status();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();
// Then add your first item
DropDownList1.Items.Insert(0, "Select");
这篇关于如何添加默认的"选取[选择这个ASP.NET DropDownList控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!