Note that there are some explanatory texts on larger screens.

plurals
  1. POstrange asp.net dropdownlist selecte value not set
    primarykey
    data
    text
    <p>I use the following code to populate my drop down list:</p> <pre><code>if (ddl_year.Items.Count == 0) { int current_year = System.DateTime.Now.Year; for (int i = 1; i &lt; 11; i++) { ListItem li = new ListItem((current_year - i).ToString(), (current_year - i).ToString()); ddl_year.Items.Add(li); } ddl_year.SelectedIndex = 0; } </code></pre> <p>then later, I use i set the selected value as follow:</p> <pre><code>ddl_year.SelectedValue="2010"; </code></pre> <p>The selected value remains <code>2012</code>; Can someone tell me what i'm doing wrong? The strange thing is that i do exactly the same thing for an other drop down list which get a selected value of <code>2009</code> and that value is shown correctly. </p> <p>Any comment will be highly appreciated.</p> <p>Thanks in advance</p> <p>More details about the actual program:</p> <pre><code>if (!Page.IsPostback) { FillYears(ddl_year_B); // The content of this method is what i desribed ealier in my post. FillYears(ddl_year_A); if (Session[CurrentCompareYearWithSessionKey] == null) Session[CurrentCompareYearWithSessionKey] = Monitor_year - 1; ddl_year_B.SelectedValue = Session[CurrentCompareYearWithSessionKey].ToString();//Works as expected if (Session[CurrentSelectedYearSessionKey] == null) Session[CurrentSelectedYearSessionKey] = Monitor_year; ddl_year_A.SelectedValue = Session[CurrentSelectedYearSessionKey].ToString();// Does not work as expected } </code></pre> <p>And below the aspx markup:</p> <pre><code>&lt;td &gt; Year:&lt;/td&gt; &lt;td &gt; &lt;asp:DropDownList ID="ddl_year_A" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_year_A_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td &gt;Compared year:&lt;/td&gt; &lt;td &gt; &lt;asp:DropDownList Width="80px" ID="ddl_year_B" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_year_B_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload