asp.net - DropDownList and Update Panel -
i develop address control, contains 2 dropdownlists (for cities , countries) , several textboxes. second dropdownlist datasource depends on first dropdownlist datasource.
<fieldset> <legend><%=title%></legend> <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <div> <label for="<%=ddlcountry.clientid %>">country</label> <asp:dropdownlist runat="server" id="ddlcountry" datatextfield="name" datavaluefield="id" datasource="<%#facade.addresses.getcountries() %>" autopostback="true" onselectedindexchanged="ddlcountry_selectedindexchanged" /> </div> <div> <label for="<%=ddlcity.clientid %>">city</label> <asp:dropdownlist runat="server" id="ddlcity" datatextfield="name" datavaluefield="name" /> </div> </contenttemplate> <triggers> <asp:asyncpostbacktrigger controlid="ddlcountry" eventname="selectedindexchanged" /> </triggers> </asp:updatepanel> <div> <label for="<%=txtstreet.clientid %>">street</label> <uc:textbox id="txtstreet" text="<%#address.street %>" runat="server" /> </div> <div> <label for="<%=txtblock.clientid %>">block</label> <uc:textbox id="txtblock" text="<%#address.block %>" runat="server" /> </div> <div> </fieldset>
code behind
protected void page_init(object sender, eventargs e) { ddlcountry.databind(); if (!ispostback) { ddlcity.datasource = facade.addresses.getcities(countryid); ddlcity.databind(); } } protected void ddlcountry_selectedindexchanged(object sender, eventargs e) { ddlcity.datasource = facade.addresses.getcities(countryid); ddlcity.databind(); }
it works good. if other control on page causes postback, when selectedvalue in ddlcity sets first (default) value.
how avoid it?
move code on page_init
page_load
, put inside !ispostback
protected void page_load(object sender, eventargs e) { if (!ispostback) { ddlcountry.databind(); ddlcity.datasource = facade.addresses.getcities(countryid); ddlcity.databind(); } }
Comments
Post a Comment