c# - dropdown_SelectedIndexChanged works second time only -
here trying populate dropdown list based on selected value dropdown list. here code both:
<div> <asp:dropdownlist id="dropdownlist1" runat="server" autopostback="true" onselectedindexchanged="dropdownlist1_selectedindexchanged"> </asp:dropdownlist> <asp:dropdownlist id="dropdownlist2" runat="server" autopostback="true"> </asp:dropdownlist> </div> </form>
and here code-behind:
protected void page_load(object sender, eventargs e) { if (!ispostback) { getdeveloperid(); } } public void getprojectid() { string projname, projid, projfinal; using (var cmdscope_id = new sqlcommand("select [projectid],[projectname] [db].[dbo].[sample table]", con)) { using (var reader = cmdscope_id.executereader()) { while (reader.read()) { projid = reader[0].tostring(); projname = reader[1].tostring(); projfinal = projid + "-" + "[" + projname + "]"; dropdownlist1.items.insert(0, new listitem(projfinal, "1")); } } } } protected void dropdownlist1_selectedindexchanged(object sender, eventargs e) { //code data db , populate dropdownlist2 }
my problem when select entry first dropdown, nothing happens first time. page reloads. if again second dropdown populates.
also, if keep if (!ispostback)
condition, nothing happens. if remove then, first dropdown populated twice , second dropdown comes. suggestions why happening?
edit: have added code populate first dropdown. same code second dropdown also.
hey problem line dropdownlist1.items.insert(0, new listitem(projfinal, "1"));
because adding item on index "0"
value 1
, compiler gets not attached selectedchange
event of dropdown.
please bind items on different index value only problem can resolved.
i have make simple program test issue. please look.
protected void page_load(object sender, eventargs e) { if (!ispostback) { fill(); } } protected void dropdownlist1_selectedindexchanged(object sender, eventargs e) { list<int> lst = new list<int>(); lst.add(12); lst.add(22); lst.add(32); dropdownlist2.datasource = lst; dropdownlist2.databind(); } private void fill() { list<int> lst = new list<int>(); lst.add(1); lst.add(2); lst.add(3); var count=0; foreach (var in lst) { dropdownlist1.items.insert(count, new listitem(i.tostring(), count.tostring())); count++; } }
hope helps you...
Comments
Post a Comment