ASP.NET DropDownList 问题:SelectedItem 未更改

发布于 2024-09-25 07:52:18 字数 5475 浏览 2 评论 0原文

我正在填充 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 1item 2item 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="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="Service Delivery Centre" Value="[email protected]"></asp:ListItem>
    <asp:ListItem Text="TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="SDC & TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC & TCS" Value="[email protected]"></asp:ListItem> 
</asp:DropDownList>

I am populating a DropDownList control as follows -

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
}

But whenever user selects a value from this dropdown, only the first item is being selected. To clarify more, suppose that the list has 4 items, item 1, item 2, item 3 and item 4. When user selects 4th item from the lists, it selects item 1 as the selected value.

What's the reason behind this?

EDIT

I have just checked the generated HTML for the DropDownList using firebug, and it seems the "selected" value doesn't change at all even if I choose different values from the DropDownList.

The generated HTML is as follows -

<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 & SDC</option>
    <option value="value5">SDC & TCS</option>
    <option value="value6">PIT & TCS</option>
    <option value="value7">PIT & SDC & TCS</option>
</select>

First, user selects a value from this dropdown list. Then he presses a button, which fires the click event. The button's corresponding event-handler function is the place where I am accessing the dropdownlist's selected value. The code is as follows -

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

The ascx file -

<%@ 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> </label>
    <asp:button ID="assignDelTeamButton" runat="server" Text="Continue" 
    CssClass="button" onclick="assignDelTeamButton_Click"/><br />
</div>

Second Edit

If I hard-code the ListItems as follows, it works perfectly -

<asp:DropDownList ID="delTeamDDList" runat="server" CssClass="select" >
    <asp:ListItem Text="Project Initiation Team" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="Service Delivery Centre" Value="[email protected]"></asp:ListItem>
    <asp:ListItem Text="TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="SDC & TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & TCS" Value="[email protected]"></asp:ListItem> 
    <asp:ListItem Text="PIT & SDC & TCS" Value="[email protected]"></asp:ListItem> 
</asp:DropDownList>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生寂 2024-10-02 07:52:18

执行以下操作之一:

  1. 在 Web.config 或包含页面中打开 ViewState(如果该状态已关闭)。
  2. 更好的是,确保 ViewState 仍处于启用状态,但在用户控件的 Init 中填充 DDL(但不要用 !IsPostBack 包装它)。这将使您的数据访问逻辑出现在每个页面/控件初始化上,甚至在回发时,但它不会向 ViewState 添加不必要的数据,因为您没有初始化 DLL,然后让 ViewState 跟踪您对其数据所做的更改来源。但是,您仍然需要 ViewState,因为 ASP.NET 中的 DDL 需要 ViewState 以便在回发时跟踪所选索引/值(如果完全关闭 ViewState,则只能通过在发布的请求的 FORM 中查找回发所选值的 DDL)名称值集合)。

Do one of the following:

  1. Turn on ViewState in Web.config or containing page if it's off there.
  2. Better yet, make sure ViewState is still enabled, but populate your DDL in your User Control's Init (but don't wrap it with !IsPostBack). This will have your data access logic on every page/control init, even on postback, but it won't add unnecessary data to ViewState as you're not init'ing your DLL, then having ViewState track the changes you make to it's data source. However, you still want ViewState because DDLs in ASP.NET require ViewState in order to track the selected index/value on postback (if you completely turn off ViewState you can only get the DDLs posted back selected value by finding it in the posted Request's FORM NameValueCollection).
以酷 2024-10-02 07:52:18

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

if you are doing this on page load make sure that you enclose it in if( !IsPostBack ){...}

獨角戲 2024-10-02 07:52:18

userControl 如何添加到页面?您是否使用 LoadControl("...Send2DeliveryTeam.ascx"); 技术动态执行此操作?如果是这样,请确保在 aspx 页面 Page_Init 处理程序中调用 LoadControl。再晚一点,ViewState 将无法应用于控件中的 ddl,并且选择将丢失。

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

How is the userControl being added to the page? Are you doing it dynamically using the LoadControl("...Send2DeliveryTeam.ascx"); technique? If so, make sure you call LoadControl in your aspx page Page_Init handler. Any later and the ViewState will fail to be applied to the ddl in the control and the selection will be lost.

Also, be aware that if you set Visible = false on the control, it will not be rendered at all. In which event are you doing this?

短叹 2024-10-02 07:52:18

尝试使用 SelectedText,而不是使用 SelectedItem。我在 C# win-forms 应用程序中遇到了同样的问题,尽管您会认为(至少我是这么认为)SelectedText 会将文本添加到下拉列表中,但它实际上会选择包含该文本的项目。

希望这有帮助。

Instead of using SelectedItem try using SelectedText. I have encounter the same problem in a C# win-forms application and although you would think (at least I did) that SelectedText would add text to the drop-down it actually selects the item with that text.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文