c# - Unable to update label text in ASCX from event handler -
i've 2 custom asp.net web form controls below:
- main.ascx
- account_selection.ascx
main contains account_selection. main acx contains event handler drop down lives on account_selection ascx.
i show error message on main ascx if there issues on contract selection. message never gets displayed. spent couple of hours never works. event handler definately gets called. i'm wondering if it's update panel or event hanlder. please help, below code snippets:
account_selection.acx has below:
<asp:updatepanel id="upcontracts" runat="server"> <contenttemplate> <asp:dropdownlist id="dropdown" runat="server" autopostback="true" onselectedindexchanged="ondropoptionselected"> <asp:listitem text="select account" value="0"></asp:listitem> </asp:dropdownlist> </contenttemplate> </asp:updatepanel>
on code behind:
public event requestcontracttypeeventhandler requestcontract; public void ondropoptionselected(string currentcontractdescription, string currentcontractnumber) { if (requestcontract != null) { requestcontract(this, new contractforaccounteventargs { contractdescription = currentcontractdescription, contract = currentcontractnumber }); } }
main ascx has below:
<div> <onlineusercontrol:contractsforaccountcontrol id="uccontractsforaccountcontrol" runat="server" /> </div> <div style="display:none" id="diverror" runat="server" class="error-container"> <asp:label id="lblmsg" cssclass="labelerror" runat="server"></asp:label> </div>
code behind:
private void initializecontrols() { uccontractsforaccountcontrol.requestcontract += new global::onlineselfservice.web.usercontrol.requestcontracttypeeventhandler(contractsforaccountcontrol_requestcontract); } void contractsforaccountcontrol_requestcontract(object sender, global::onlineselfservice.web.usercontrol.contractforaccounteventargs e) { this.lblmsg.text="something bad happened!; //this line doesn't displayed this.diverror.style.add("display", ""); //in debugger line shows "cannot inner content of diverror because contents not literal" }
it update panel causing issue. way update panel works only content within update panel updated. cannot update controls outside of update panel initiated "partial post back". reason called "partial post back" because render called child controls within update panel. more accurate description happens "full post" (all input control values on page sent in request aka post) "partial back" (response) contains markup items within update panel. fact input control values (including hidden viewstate input control) sent in request makes update panel more expensive json ajax call posts values needed response (in case selected account) less expensive full post because markup controls in update panel (plus viewstate) returned in response.
possible solutions: option 2 recommendation, not sure how many other pages use control, , need update each of these pages add updatepanel.
option 1: move error label within updatepanel in account_selection.ascx. option not if want update other data outside of updatepanel when selected account changed.
<asp:updatepanel id="upcontracts" runat="server"> <contenttemplate> <asp:dropdownlist id="dropdown" runat="server" autopostback="true" onselectedindexchanged="ondropoptionselected"> <asp:listitem text="select account" value="0"></asp:listitem> </asp:dropdownlist> <div style="display:none" id="diverror" runat="server" class="error-container"> <asp:label id="lblmsg" cssclass="labelerror" runat="server"></asp:label> </div> </contenttemplate> </asp:updatepanel>
code behind:
public event requestcontracttypeeventhandler requestcontract; public void ondropoptionselected(string currentcontractdescription, string currentcontractnumber) { if (requestcontract != null) { try{ requestcontract(this, new contractforaccounteventargs { contractdescription = currentcontractdescription, contract = currentcontractnumber }); } catch(exception ex){ this.lblmsg.text="something bad happened!"; this.diverror.style.add("display", ""); } } }
option 2: remove updatepanel "account_selection" user control, , move aspx page , wrap updatepanel in aspx around content want update.
your aspx page this:
<asp:updatepanel id="upcontracts" runat="server"> <contenttemplate> <onlineusercontrol:maincontroltagname id="mymaincontrol" runat="server" /> </contenttemplate> </asp:updatepanel>
Comments
Post a Comment