asp.netdrop-down-menuwebformsautopostbackispostback

dropdownlist always returns first value even with EnableViewState set to true


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 :

Click to view

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,


Solution

  • So I resolved the issue after almost going insane, the problem weirdly enough was with getMajDetails() implemented in the WebserviceRobots. This method is used to deserialise an XML document containing updates versions and some other informations, and to return a string with these elements seprated by | character. However, it seems like XDocument automatically adds new line char at the end of each element, resulting in a string similar to this one :

    1.7.1940\r\n| Détails concernant le MAJ 1.7\r\n| Commentaires 1.7\r\n||2.0.2542\r\n| Détails concernant le MAJ 2.0\r\n| Commentaires 2.0\r\n||1.8.2495\r\n| Détails concernant le MAJ 1.8\r\n| Commentaires 1.8\r\n||

    So, each ListeVersionsMAJs element ends up with \r\n at the end. and When this list is used to populate the dorpdown list, it causses it to behave the way it did (returning the first value at each postback). the solution was to remove \r\n . I have no explanation as to why this is happening, testing the dropdown list with a List of strings initialized with fixed string values with new line char at the end of each value resulted in the same odd behavior. So if anyone could clear this up, it would be much appreciate it.