简体   繁体   English

如何向此ASP.NET DropDownList控件添加默认的“选择”选项?

[英]How to add a default “Select” option to this ASP.NET DropDownList control?

I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. 我是ASP.NET的新开发人员,正在尝试学习Linq-To-Entities。 I am trying to bind a DropDownList with the Linq statement for retrieving the list of status in the Status Entity. 我试图将一个DropDownList与Linq语句绑定,以获取状态实体中的状态列表。 Everything is working fine. 一切正常。 However, I am trying now to add "Select" option to the DropDownList but It doesn't work with me. 但是,我现在正在尝试向DropDownList添加“选择”选项,但是它对我不起作用。 Could you please tell me how to fix this? 你能告诉我如何解决这个问题吗?

ASP.NET Code: ASP.NET代码:

<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 我也尝试在DropDownList的标记集中执行此操作,但它对我也不起作用

<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. 它不起作用的原因是,您要向列表中添加一个项目,然后使用新的DataSource覆盖整个列表,该DataSource将清除并重新填充列表,从而丢失第一个手动添加的项目。

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");

Although it is quite an old question, another approach is to change AppendDataBoundItems property. 尽管这是一个很老的问题,但是另一种方法是更改AppendDataBoundItems属性。 So the code will be: 因此,代码将是:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                  AppendDataBoundItems="True">
     <asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem>
</asp:DropDownList>

I have tried with following code. 我尝试了以下代码。 it's working for me fine 对我来说很好

ManageOrder Order = new ManageOrder();
Organization.DataSource = Order.getAllOrganization(Session["userID"].ToString());
Organization.DataValueField = "OrganisationID";
Organization.DataTextField = "OrganisationName";                
Organization.DataBind();                
Organization.Items.Insert(0, new ListItem("Select Organisation", "0"));

Move DropDownList1.Items.Add(new ListItem("Select", "0", true)); 移动DropDownList1.Items.Add(new ListItem(“ Select”,“ 0”,true)); After bindStatusDropDownList(); 在bindStatusDropDownList()之后;

so: 所以:

    if (!IsPostBack)
    {
        bindStatusDropDownList(); //first create structure
        DropDownList1.Items.Add(new ListItem("Select", "0", true)); // after add item
    }

If you do an "Add" it will add it to the bottom of the list. 如果您执行“添加”,它将添加到列表的底部。 You need to do an "Insert" if you want the item added to the top of the list. 如果要将项目添加到列表的顶部,则需要执行“插入”。

Private Sub YourWebPage_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete

     If Not IsPostBack Then
     DropDownList1.Items.Insert(0, "Select")
     End If

End Sub

If you want to make the first item unselectable, try this: 如果要使第一项变为不可选择,请尝试以下操作:

DropDownList1.Items.Insert(0, new ListItem("Select", "-1"));
DropDownList1.Items[0].Attributes.Add("disabled", "disabled");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM