我填充DropDownList控件如下 -ASP.NET DropDownList的問題:的SelectedItem沒有改變
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
作爲選定值。
這是什麼原因?
編輯
我剛纔檢查生成的HTML使用螢火蟲DropDownList的,而且似乎「選擇」值不能使用,即使我選擇從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 & 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="[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>
呈現的HTML看起來像什麼?什麼是呈現的關鍵/值對?這是說 - 基於一個集合循環時要小心,並添加一個基於該集合和另一個鍵/值。 – RPM1984 2010-09-28 04:52:04
@RPM:請參閱編輯。 – 2010-09-28 05:01:25
@Night Shade - 你能提供原始的HTML嗎?我想看看實際的HTML看起來像什麼,然後再進入「選定的項目」。讓我們看看它的有效html。即時銀行沒有。 – RPM1984 2010-09-28 05:04:14