c# - Maintain selected radio button item on postback -


i want populate radiobuttonlist 5 values database in random order.

so i've tried store 5 values in session objects , used following code add values radiobuttonlist (rblquestions) randomly:

    random ran = new random();     var numbers = enumerable.range(1, 5).orderby(i => ran.next()).tolist();      list<listitem> ans = new list<listitem>();     ans.add(new listitem(session["value1"].tostring(), "y"));     ans.add(new listitem(session["value2"].tostring(), "n"));     ans.add(new listitem(session["value3"].tostring(), "n"));     ans.add(new listitem(session["value4"].tostring(), "n"));     ans.add(new listitem(session["value5"].tostring(), "n"));      foreach (int num in numbers)     {         rblquestions.items.add(ans[num - 1]);     } 

this works great. however, when click on button on same form, selected item in list changes randomly. why , how can avoid this?

[edit include code.]

<form id="form1" runat="server"> <div>     <asp:button id="btngetquestion" runat="server" text="get question" onclick="btngetquestion_click" />     <asp:label id="lblquestion" runat="server" text=""></asp:label>     <asp:radiobuttonlist id="rblquestions" runat="server"></asp:radiobuttonlist>     <asp:button id="btncheck" runat="server" text="check answer" onclick="btncheck_click" />     <asp:label id="lblanswer" runat="server" text=""></asp:label>     <asp:label id="lblerror" runat="server" text=""></asp:label> </div> </form> 

c#:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using mysql.data.mysqlclient; using system.configuration;  public partial class questions : system.web.ui.page { protected void page_load(object sender, eventargs e) { }  protected void btngetquestion_click(object sender, eventargs e) {     getrandomquestion(); }  protected void btncheck_click(object sender, eventargs e) {  }  protected void getrandomquestion() {     string connstr = configurationmanager.connectionstrings["myconnectionstring"].connectionstring;     mysqlconnection conn = new mysqlconnection(connstr);     mysqldatareader reader;      try     {         conn.open();          string cmdtext = "select * questions order rand() limit 1";         mysqlcommand cmd = new mysqlcommand(cmdtext, conn);          reader = cmd.executereader();          if (reader.read())         {             lblquestion.text = reader["question"].tostring();             session["answer1"] = reader["answer1"].tostring();             session["answer2"] = reader["answer2"].tostring();             session["answer3"] = reader["answer3"].tostring();             session["answer4"] = reader["answer4"].tostring();             session["answer5"] = reader["answer5"].tostring();         }         else         {             lblerror.text = "(no questions found)";         }         reader.close();     }     catch     {         lblerror.text = "database connection error - failed insert record.";     }         {         conn.close();     }      random ran = new random();     var numbers = enumerable.range(1, 5).orderby(i => ran.next()).tolist();      list<listitem> ans = new list<listitem>();     ans.add(new listitem(session["answer1"].tostring(), "y"));     ans.add(new listitem(session["answer2"].tostring(), "n"));     ans.add(new listitem(session["answer3"].tostring(), "n"));     ans.add(new listitem(session["answer4"].tostring(), "n"));     ans.add(new listitem(session["answer5"].tostring(), "n"));      foreach (int num in numbers)     {         rblquestions.items.add(ans[num - 1]);     } } } 


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -