gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:25:10 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com