ASP.NET DropDownList 问题 : SelectedItem isn't changing

标签 asp.net drop-down-menu

我正在填充一个 DropDownList 控件,如下所示 -

public partial class UserControls_PMS_Send2DeliveryTeam : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            // SA 100928 Get the delivery teams and their respective email addresses
            string[] delTeam = ConfigurationManager
                               .AppSettings["deliveryTeamNames"]
                               .Split(',');
            string[] delTeamEmails = ConfigurationManager
                                     .AppSettings["deliveryTeamEmails"]
                                     .Split('|');

            if (delTeam.Length != delTeamEmails.Length)
            {
                showAlert("You have an error in the configuration of the delivery teams");
                return;
            }

            for(int looper=0; looper<delTeam.Length; looper++)
                delTeamDDList
                .Items
                .Add
                ( 
                    new ListItem(delTeam[looper], delTeamEmails[looper])
                );

        }

    // Other methods
}

但是每当用户从此下拉列表中选择一个值时,只会选择第一个项目。为了进一步澄清,假设列表有 4 个项目,item 1 , item 2 , item 3item 4 .当用户从列表中选择第 4 项时,它选择 item 1作为选定的值。

这背后的原因是什么?

编辑

我刚刚使用 firebug 检查了 DropDownList 生成的 HTML,即使我从 DropDownList 中选择了不同的值,似乎“选定”值也根本没有改变。

生成的 HTML 如下 -
<select class="select" id="Send2DeliveryTeam_delTeamDDList" name="Send2DeliveryTeam$delTeamDDList">
    <option value="value1" selected="selected">Project Initiation Team</option>
    <option value="value2">Service Delivery Centre</option>
    <option value="value3">TCS</option>
    <option value="value4">PIT &amp; SDC</option>
    <option value="value5">SDC &amp; TCS</option>
    <option value="value6">PIT &amp; TCS</option>
    <option value="value7">PIT &amp; SDC &amp; TCS</option>
</select>

首先,用户从此下拉列表中选择一个值。然后他按下一个按钮,触发点击事件。按钮对应的事件处理函数是我访问下拉列表选定值的地方。代码如下——
// Button event-handler code
protected void assignDelTeamButton_Click(object sender, EventArgs e)
{
    // This is where I am always getting the same value, no matther what I choose
    // from the dropdown list, and this value is the one which is selected by default
    // when the page loads. I mean, the "SelectedIndex" is always 0.
    string selected_value = delTeamDDList.SelectedItem.ToString();

    // Other  codes
}

ascx 文件 -
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Send2DeliveryTeam.ascx.cs" Inherits="UserControls_PMS_Send2DeliveryTeam" %>
<div id="Common">
    <h3>Welcome <%string user = HttpContext.Current.Session["user_name"].ToString();%><%=user %></h3>
    <h1>Request Estimate Screen</h1>
    <span>Request Estimate and Assign a Delivery team to a Request</span><br />
    <label>Enter an existing project number</label>
    <asp:TextBox ID="reqNum" runat="server" CssClass="textBox" /><br />
    <label>Select Delivery Team</label>
    <asp:DropDownList ID="delTeamDDList" runat="server" CssClass="select" >

    </asp:DropDownList>
    <label> - Sorted in alpha order</label><br /><br />
    <label>&nbsp;</label>
    <asp:button ID="assignDelTeamButton" runat="server" Text="Continue" 
    CssClass="button" onclick="assignDelTeamButton_Click"/><br />
</div>

第二次编辑

如果我按如下方式对 ListItems 进行硬编码,它就可以完美运行 -
<asp:DropDownList ID="delTeamDDList" runat="server" CssClass="select" >
    <asp:ListItem Text="Project Initiation Team" Value="email1@yahoo.com"></asp:ListItem> 
    <asp:ListItem Text="Service Delivery Centre" Value="email2@yahoo.com"></asp:ListItem>
    <asp:ListItem Text="TCS" Value="email3@yahoo.com"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC" Value="email4@yahoo.com"></asp:ListItem> 
    <asp:ListItem Text="SDC & TCS" Value="email5@yahoo.com"></asp:ListItem> 
    <asp:ListItem Text="PIT & TCS" Value="email6@yahoo.com"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC & TCS" Value="email7@yahoo.com"></asp:ListItem> 
</asp:DropDownList>

最佳答案

如果您在页面加载时执行此操作,请确保将其包含在 if( !IsPostBack ){...}

关于ASP.NET DropDownList 问题 : SelectedItem isn't changing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3809755/

相关文章:

drop-down-menu - 在 Qt 中创建自定义上下文菜单

asp.net-mvc - Kendo UI DropDownListFor html 包装器 - 如何将 BindTo 设置到模型中的集合?而不是 ViewData 或 Viewbag

javascript - 路由 asp.net mvc 中的 AJAX 错误

javascript - 我可以使用JavaScript删除ASP.NET身份验证cookie(.ASPXAUTH)吗?

css - 垂直对齐 CSS3 下拉菜单

javascript - 移动点击下拉菜单不起作用?

jquery - 使用 jquery 检查下拉选项选择框的最后一个值

.net - 所有单选按钮组是否被点击

.net - 配置多个 .Net 应用程序的最佳方法

c# - 使用 XmlReader.Create(uri) 防止或处理超时