estoy poblando un control DropDownList de la siguiente manera -ASP.NET DropDownList problema: SelectedItem no está cambiando
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
}
Pero cada vez que el usuario selecciona un valor de este menú desplegable, se está seleccionando sólo el primer elemento. Para aclarar más, suponga que la lista tiene 4 elementos, item 1
, item 2
, item 3
y item 4
. Cuando el usuario selecciona el 4º elemento de las listas, selecciona item 1
como el valor seleccionado.
¿Cuál es la razón detrás de esto?
EDITAR
he acaba de comprobar el código HTML generado para el DropDownList utilizando Firebug, y parece que el valor "seleccionado" no cambia en absoluto, incluso si decido valores diferentes de la DropDownList.
El HTML generado es el siguiente -
<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>
En primer lugar, el usuario selecciona un valor de la lista desplegable. Luego presiona un botón, que dispara el evento click. La función del controlador de eventos correspondiente al botón es el lugar donde estoy accediendo al valor seleccionado de la lista desplegable. El código es el siguiente -
// 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
}
la 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>
Segunda Edición
Si I código el ListItems de la siguiente manera, funciona perfectamente -
<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>
¿cómo se ve el HTML procesado? ¿Cuál es el par clave/valor que se procesa? Dicho esto: tenga cuidado al realizar un bucle basado en una colección, y agregue una clave/valor basado en esa colección Y otra. – RPM1984
@RPM: Consulte la edición. –
@Night Shade: ¿puede proporcionar el HTML sin formato? Quiero ver cómo se ve el HTML real antes de ingresar al "elemento seleccionado". veamos si es válido html. estoy bancario en no. – RPM1984