My dropdown list control always returns the first item on postback, I've tried every solution i've come across but to no avail. Bascially, I have two data classes that are simply containers for data. ConnectedRobots (representing connected robot with its controller's version, its IP address and some other properties), and MiseAJour (representing a single available update with its version and some other details describing it). These two classes are then used to create objects representing each connected robot or update available. I'm then creating a List of update versions to use it as a data source for each Dropdown list. as shown in the picture below :
Here's the piece of code reflecting this view (Default.aspx) :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationTest.WebForm1" Theme="Theme1" EnableEventValidation="false" EnableViewState="true"%>
...
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
...
<div style="width:100%;">
<table class="table table-hover">
<thead>
<tr>
<th class="auto-style1">Référence Robot/Version Contrôleur</th>
<th class="auto-style1">Pays/Adresse IP</th>
<th class="auto-style1">Etat</th>
<th class="auto-style1">Versions des contrôleurs disponibles</th>
<th class="auto-style1">Planification</th>
<th>MAJ</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="repCRobots" runat="server" EnableViewState="true">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblReferenceRobot" runat="server" Text='<%# Eval("ReferenceRobot") + " / " %> ' />
<asp:Label ID="lblVersionControleur" runat="server" Font-Bold="true" Text='<%# Eval("VersionControleur") %>'/>
</td>
<td>
<asp:Label ID="lblPays" runat="server" Text='<%# Eval("Pays") + " / " %> ' />
<asp:Label ID="lblIPRobot" runat="server" Font-Bold="true" Text='<%# Eval("IPRobot") %>'/>
</td>
<td>
<asp:Label ID="lblEtat" runat="server" Font-Bold="true" Text='<%# Eval("Etat") %>'/>
</td>
<td>
<asp:DropDownList ID="VersionsMAJs" runat="server" CssClass="bg-primary" EnableViewState="true" OnSelectedIndexChanged="VersionsMAJs_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
<td>
<span style="padding: 0px 10px 10px 10px">
<cc1:TimeSelector ID="TimeSelector1" runat="server" DisplayButtons="false" Font-Bold="true" BackColor="#cce6ff" BorderStyle="Dotted" CssClass="bg-info" BorderColor="White"></cc1:TimeSelector>
</span>
</td>
<td>
<asp:CheckBox ID='SelectMAJ' runat="server"/>
<asp:HiddenField ID="HiddenCheckBox" Value='<% #Eval("ReferenceRobot")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<asp:Button ID="MAJButton" runat="server" Text="Mettre à jour" CssClass="btn btn-success btn-lg btn-block"/>
</div>
And the code behind is the following :
public partial class WebForm1 : System.Web.UI.Page
{
protected List<MiseAJour> ListeMAJs;
protected List<string> ListeVersionsMAJs;
protected List<ConnectedRobots> ListeRobotsConnectes;
protected List<string> ListeRefRobots;
protected DropDownList ddlVersionMAJ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetConnectedRobots();
Repeater repCRobots = this.FindControl("repCRobots") as Repeater;
repCRobots.DataSource = ListeRobotsConnectes;
repCRobots.DataBind();
foreach (RepeaterItem item in repCRobots.Items)
{
ddlVersionMAJ = item.FindControl("VersionsMAJs") as DropDownList;
ddlVersionMAJ.DataSource = ListeVersionsMAJs;
ddlVersionMAJ.DataBind();
}
}
}
private void GetConnectedRobots()
{
WebserviceRobots ws = new WebserviceRobots();
string[] delimiters = new string[] { "|", "||" };
string connectedRobots = ws.getConnectedRobots();
string majsDetails = ws.getMajsDetails();
string[] connectedRobotsInfos = connectedRobots.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string[] majsInfos = majsDetails.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
ListeRobotsConnectes = new List<ConnectedRobots>();
ListeRefRobots = new List<string>();
ListeMAJs = new List<MiseAJour>();
ListeVersionsMAJs = new List<string>();
for (int i = 0; i < majsInfos.Length - 1; i += 3)
{
ListeMAJs.Add(new MiseAJour() { VersionMAJ = majsInfos[i], DetailsMAJ = majsInfos[i + 1], Commentaires = majsInfos[i + 2] });
}
ListeVersionsMAJs = ListeMAJs.Select(v => v.VersionMAJ).ToList();
for (int i = 0; i < connectedRobotsInfos.Length - 1; i += 5)
{
ListeRobotsConnectes.Add(new ConnectedRobots() { ReferenceRobot = connectedRobotsInfos[i], VersionControleur = connectedRobotsInfos[i + 1], IPRobot = connectedRobotsInfos[i + 2], Pays = connectedRobotsInfos[i + 3], Etat = connectedRobotsInfos[i + 4], VersionsMAJ = ListeVersionsMAJs });
}
ListeRefRobots = ListeRobotsConnectes.Select(r => r.ReferenceRobot).ToList();
}
protected void VersionsMAJs_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dll = (DropDownList)sender;
string test = dll.SelectedValue;
}
the GetConnectedRobots() method is used to retrieve both update versions, and connected robots using a web service and couple of dynamically generated xml files. On selectedIndexChanged, I should get the selected value or so I thought. SelectedValue always returns the first element of the dropdown list. What am I doing wrong? I do have EnableViewState="true" for persistence between postbacks. I am open to all kinds of answers, even if it means restructuring the whole code, Thank you for your answers,