我正在按照以下方式填充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 3
和item 4
。当用户从列表中选择第4个项目时,它会将item 1
作为所选值。这是什么原因呢? 编辑 我刚刚使用Firebug检查了DropDownList生成的HTML,似乎即使我从DropDownList选择不同的值,"selected"值也没有改变。
生成的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 & SDC</option>
<option value="value5">SDC & TCS</option>
<option value="value6">PIT & TCS</option>
<option value="value7">PIT & SDC & 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> </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>