c# - TextChanged event does not fire when 2nd modification equals the original value -
i have _textchanged event works except in specific circumstance can replicated follows:
- user modifies text (event fires correctly)
- user modifies text again match original value (event doesn't fire)
i can _textchanged event work on development box turning on viewstate update panel on ascx page, when move server error viewstate failed if switch user controls , switch page. controls go inside update panel build dynamically in code behind , rebuilt each postback -- works every other postback don't think issue controls.
additionally, turning on viewstate makes page run dreadfully slow anyway, not ideal fix.
finally, _textchanged event works changes except when reverting original value.
can tell me why event doesn't fire in specific circumstance, , how address problem?
text box creation in code behind:
textbox annualhourstextbox = new textbox(); annualhourstextbox.id = string.format("bundle{0}_annualhourstextbox{1}", bundle.bundlenbr, parentitem.laboritemnbr); annualhourstextbox.cssclass = ""; annualhourstextbox.columns = 4; annualhourstextbox.text = childitem == null ? string.empty : childitem.ftehours.tostring("f0"); annualhourstextbox.autopostback = true; annualhourstextbox.textchanged += new eventhandler(annualhourstextbox_textchanged); asyncpostbacktrigger ahtrigger = new asyncpostbacktrigger(); ahtrigger.controlid = annualhourstextbox.uniqueid; ahtrigger.eventname = "textchanged"; uppricingsheet.triggers.add(ahtrigger); //snip //add attributes reference on events annualhourstextbox.attributes["othercontrol"] = tasksperyeartextbox.uniqueid; annualhourstextbox.attributes["nextcontrol"] = benefitstextbox.uniqueid; annualhourstextbox.attributes["targettbcontrol"] = tasktimetextbox.uniqueid; annualhourstextbox.attributes["targetddlcontrol"] = tasktimeuomdropdown.uniqueid; event handler:
protected void annualhourstextbox_textchanged(object sender, eventargs e) { textbox ah = sender textbox; textbox other = page.findcontrol(ah.attributes["othercontrol"]) textbox; if ((!string.isnullorempty(ah.text)) && (!string.isnullorempty(other.text))) { textbox next = page.findcontrol(ah.attributes["nextcontrol"]) textbox; textbox targettb = page.findcontrol(ah.attributes["targettbcontrol"]) textbox; dropdownlist ddl = page.findcontrol(ah.attributes["targetddlcontrol"]) dropdownlist; double taskspersecond; taskspersecond = calculatetimepertask(ah.text, other.text); string timeunit; double time; if (taskspersecond < 60) { timeunit = "seconds"; time = taskspersecond; } else if (taskspersecond < 3600) { timeunit = "minutes"; time = (taskspersecond / 60); } else { timeunit = "hours"; time = (taskspersecond / 60 / 60); } //enter time in appropriate textbox targettb.text = time.tostring("f2"); //select appropriate item ddl listitem = ddl.items.findbytext(timeunit); if (i != null) { ddl.selecteditem.selected = false; i.selected = true; } } } aspx page:
<%@ page title="" language="c#" masterpagefile="~/masterpage.master" autoeventwireup="true" codefile="solution.aspx.cs" inherits="solution" %> <%@ register src="fragments/solutionrecommended.ascx" tagname="solutionrecommended" tagprefix="uc1" %> <%@ register src="fragments/solutionpricingsheet.ascx" tagname="solutionpricingsheet" tagprefix="uc2" %> <%@ register src="fragments/solutionsuggested.ascx" tagname="solutionsuggested" tagprefix="uc3" %> <%@ register src="fragments/solutionsummary.ascx" tagname="solutionsummary" tagprefix="uc4" %> <%@ register src="fragments/ucitemfiltersearch.ascx" tagname="ucitemfiltersearch" tagprefix="uc5" %> <asp:content id="content1" contentplaceholderid="head" runat="server"> <script type="text/javascript"> function additemtobundle(posturl, redirecturl) { $.post(posturl); window.location = redirecturl; // window.location = url; } </script> </asp:content> <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server"> <asp:hiddenfield id="hfstepnbr" runat="server" /> <asp:panel id="pnlstepmessage" runat="server" visible="false" cssclass="padding10"> <h3 class="placeholder"> <asp:label id="lblmessage" runat="server" /></h3> </asp:panel> <div class='elev8form' id="maindiv" runat="server"> <h3 class='header'> solutions</h3> <div id="tabs"> <div class='tab'> <asp:linkbutton id="lbsuggested" runat="server" text="select items" data-step="1" onclick="lbtab_click" causesvalidation="false"></asp:linkbutton> </div> <div class='tab'> <asp:linkbutton id="lbpricing" runat="server" text="pricing worksheet" data-step="2" onclick="lbtab_click" ></asp:linkbutton> </div> <div class='tab'> <asp:linkbutton id="lbrecommendedsolutions" runat="server" text="recommended solutions" data-step="3" onclick="lbtab_click" causesvalidation="false"></asp:linkbutton> </div> <div class='tab'> <asp:linkbutton id="lbsummary" runat="server" text="solutions summary" data-step="4" onclick="lbtab_click" causesvalidation="false"></asp:linkbutton> </div> </div> <div id="solutions-body"> <asp:multiview id="mltsolution" runat="server"> <asp:view id="viewsuggested" runat="server"> <uc3:solutionsuggested id="solutionsuggested1" runat="server" redirecturl="~/portal/elev8/solution.aspx" /> </asp:view> <asp:view id="viewpricing" runat="server"> <uc2:solutionpricingsheet id="solutionpricingsheet1" runat="server" /> </asp:view> <asp:view id="viewrecommended" runat="server"> <uc1:solutionrecommended id="solutionrecommended1" runat="server" /> </asp:view> <asp:view id="viewsummary" runat="server"> <p style="font-size: 14px;"> text here </p> <uc4:solutionsummary id="solutionsummary1" runat="server" /> </asp:view> </asp:multiview> </div> </div> <script type="text/javascript"> function pageload() { $(function () { var maxchannelheight; var items = $('.channel'); (var counter = 0; counter < items.length; counter++) { var channel = items[counter]; var channelheight = $(channel).height(); maxchannelheight = maxchannelheight > channelheight ? maxchannelheight : channelheight; } $('.channel').height(maxchannelheight); $("#priceing-sheet-save-button *").click(function () { window.scrollto(0, 0); }); }); } </script> ascx page:
<%@ control language="c#" autoeventwireup="true" codefile="solutionpricingsheet.ascx.cs" inherits="solutionpricingsheet" %> <asp:updateprogress id="upprogressrecsolution" runat='server' associatedupdatepanelid="uppricingsheet"> <progresstemplate> <div style="position: absolute; z-index: 2000; left: 45%; display: inline; width: 100px;" class="elev8form"> <asp:image id="image1" runat='server' imageurl="~/portal/img/ajax-loader-big.gif" /> </div> </progresstemplate> </asp:updateprogress> <div id="pricing-sheet-wrapper"> <p class='left'> more text</p> <asp:panel id="pnlsavemessage" runat="server" visible="false" cssclass="save-message"> <span>item prices saved</span> </asp:panel> <div class='export'> <span class='bigbutton'> <asp:linkbutton id="btnexport" runat='server' text="export excel" onclick="btnexport_click" /> </span> </div> <asp:updatepanel id="uppricingsheet" runat="server" updatemode="conditional" viewstatemode="disabled"> <contenttemplate> <div id="pricing-sheet"> <asp:placeholder id="phcontent" runat="server"></asp:placeholder> <asp:placeholder id="opportunityplaceholder" runat="server" /> <div class='save export'> <div> <div id="pagevalidationerror" class="validationmessage"> * changes not saved. review entries validation messages. required fields marked asterisk. </div> </div> <%--<asp:hiddenfield id="hf" runat="server" value="0" />--%> <center> <span id="priceing-sheet-save-button"> <asp:button id="btnsave" runat="server" text="save prices" skinid="redbutton" onclick="btnsave_click" causesvalidation="true" /> </span> </center> </div> </div> <script type="text/javascript"> function pageload() { $("#tabs .tab a").click(function () { $("#<%= btnsave.clientid%>").click(); }); } </script> </contenttemplate> </asp:updatepanel> </div> <script type="text/javascript"> $(document).ready(function () { $('.validationmessage').hide(); $('#<%= btnsave.clientid %>').click(function () { if (page_isvalid == false) { $('.validationmessage').show(); return false; } }); $('input[type=text]').blur(function () { if (page_isvalid == false) { $('.validationmessage').show(); return false; } else { $('.validationmessage').hide(); } }) });
that intended behavior - event called ontextchanged (different original) not ontexttyped (any text entered), have handle event (which triggers if nothing @ entered):
onblur="__dopostback(this.id, '');"
update: pretty simple actually, since using ajax, textbox's .defaultvalue not changing between postbacks, .value - either use onblur told you, or on every postback change .defaultvalue .value in javascript: http://www.w3schools.com/jsref/prop_text_defaultvalue.asp
or place textbox in updatepanel, , take care of self on own...
update 2: first off, in code textbox shown inside `updatepanel', , secondly, have 3 choices:
a) onblur method work, remove autopostback property (it client side onchange event), keep ontextchanged event (it server side).
b) viewstate method work, set viewstatemode="enabled" on textbox, , make sure using viewstatemode="disabled" on containers - , not enableviewstate="false".
c) javascript .defaultvalue method...
Comments
Post a Comment