c# - Setting SelectedValue of DDL on PageLoad using value saved in Session Variable -
i'm trying set selected value of dropdownlist on page_load using value saved session variable. dropdownlist populated , value stored along other info in session. i'm creating edit entry page , want have of fields populated info session.
what i've done populate textbox session variables, advisors section need user enter number instead of name. need use dropdownlist make sure information entered accurately. i've looked methods of databinding, databound, using data source, etc seemed of dropdownlists generated dynamically. appreciated.
my .aspx code:
<asp:textbox runat="server" id="fname" maxlength="100"></asp:textbox> <asp:dropdownlist id="advisors" runat="server"> <asp:listitem value="">select advisor</asp:listitem> <asp:listitem value="2">joe schmo</asp:listitem> </asp:dropdownlist>
code behind:
protected void page_load(object sender, eventargs e) { if (!ispostback) { fname.text = session["fname"].tostring(); lname.text = session["lname"].tostring(); sex.text = session["sex"].tostring(); sid.text = session["sid"].tostring(); email.text = session["email"].tostring(); phone.text = session["phone"].tostring(); advisors.selectedvalue = session["advisor"].tostring(); } }
update:
so i'm dumb. session variable storing name "joe schmo" not value "2". below code worked me.
foreach (listitem listitem in advisors.items) { listitem.selected = listitem.text.equals(session["advisor"].tostring()); }
try this
foreach (listitem listitem in advisors.items) { if(listitem.text.equals(session["advisor"].tostring()) dropdownlist1.items.findbyvalue(session["advisor"].tostring()).selected = true; }
Comments
Post a Comment