代码之家  ›  专栏  ›  技术社区  ›  MD Sayem Ahmed

ASP.NETDropDownList问题:SelectedItem未更改

  •  5
  • MD Sayem Ahmed  · 技术社区  · 14 年前

    我正在填充一个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
    }
    

    item 1 , item 2 , item 3 item 4 项目1 作为选定值。

    这背后的原因是什么?

    编辑

    我刚刚使用firebug检查了DropDownList生成的HTML,似乎“selected”值根本没有改变,即使我从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>
    

    首先,用户从这个下拉列表中选择一个值。然后他按下一个按钮,触发click事件。按钮对应的事件处理函数是我访问dropdownlist的选定值的地方。代码如下-

    // 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>
    

    第二次编辑

    如果我硬编码列表项如下,它工作得很好-

    <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>
    
    4 回复  |  直到 14 年前
        1
  •  8
  •   Ted    14 年前

    执行下列操作之一:

    1. 在中启用ViewStateWeb.config文件或包含页面,如果它在那里。
    2. 更好的是,确保ViewState仍然处于启用状态,但是在用户控件的Init中填充DDL(但是不要用!IsPostBack)。这将使您的数据访问逻辑在每个页面/控件初始化,甚至在回发时,但它不会添加不必要的数据到ViewState,因为您没有初始化DLL,然后让ViewState跟踪您对其数据源所做的更改。但是,您仍然需要ViewState,因为DDL在ASP.NET需要ViewState以便在回发时跟踪选定的索引/值(如果完全关闭ViewState,则只能通过在已发布请求的表单NameValueCollection中找到DDLs回发的选定值来获取该值)。
        2
  •  8
  •   Vinay B R    14 年前

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

        3
  •  4
  •   Daniel Dyson    14 年前

    如何将userControl添加到页面?你是不是在用 LoadControl("...Send2DeliveryTeam.ascx"); 技术?如果是这样,请确保在aspx页面初始化处理程序中调用LoadControl。以后,ViewState将无法应用于控件中的ddl,并且选择将丢失。

    另外,请注意,如果在控件上设置Visible=false,则根本不会呈现该控件。你在哪种情况下这样做?

        4
  •  2
  •   Gage    14 年前

    SelectedItem 尝试使用 SelectedText . 我在一个C#win表单应用程序中遇到了同样的问题,尽管你会认为(至少我是这样认为的) 将文本添加到下拉列表中,它实际选择了包含该文本的项。

    希望这有帮助。