c# - javascript not working when updating the panel -
there several tabs in page , in 1 tab trying change other drop down when selecting first drop down. using updatepanel , script manager that. problem wrote datepicker javascript function , works fine first time if not select drop down box when select drop down box javacript not work. helpful if can identify doing wrong.
<asp:scriptmanager id="scriptmanager1" runat ="server"></asp:scriptmanager> <div id="requesthistory" class="tab-pane"> <asp:updatepanel id="updatepanelcrhistory" runat="server" cssclass="row" defaultbutton="btnsearch" updatemode="conditional"> <contenttemplate> <div class="colmd-3 col-sm-3 col-xs-3 responsive-filterbar"> <asp:dropdownlist runat="server" id="drpcrhistoryframework" clientidmode="autoid" datatextfield="title" datavaluefield="frameworkid" cssclass="form-control" autopostback="true" onselectedindexchanged="drpcrhistoryframework_selectedindexchanged"> <asp:listitem selected="true" text ="--framework--" value="0" ></asp:listitem> </asp:dropdownlist> </div> <div class="colmd-3 col-sm-3 col-xs-3 responsive-filterbar"> <asp:dropdownlist runat="server" id="drpcrhskillname" clientidmode="autoid" datatextfield="skillname" datavaluefield="skillid" cssclass="form-control" autopostback="true" onselectedindexchanged="drpcrhskillname_selectedindexchanged"> </asp:dropdownlist> </div> <div class="colmd-3 col-sm-3 col-xs-3 responsive-filterbar"> <asp:dropdownlist runat="server" id="drpcrhlevel" clientidmode="autoid" cssclass="form-control" autopostback="true" onselectedindexchanged="drpcrhlevel_selectedindexchanged"> </asp:dropdownlist> </div> <div class="colmd-3 col-sm-3 col-xs-3 responsive-filterbar input-group date" style="padding-left: 15px;" id="dpdate"> <asp:textbox runat="server" id="txtfromdate" clientidmode="static" textmode="singleline" cssclass="form-control datepicker" placeholder="0"></asp:textbox> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> <asp:textbox runat="server" id="txttodate" clientidmode="static" textmode="singleline" cssclass="form-control datepicker" placeholder="0"></asp:textbox> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div><br /><br /> <div class="col-md-3 col-sm-12 col-xs-12 responsive-filterbar"> <div class="input-group"> <asp:textbox runat="server" id="canidaterequesthistorysearchtextbox" clientidmode="static" cssclass="form-control"></asp:textbox> <span class="input-group-btn"> <asp:linkbutton runat="server" id="canidaterequesthistorysearchlinkbutton" clientidmode="static" onclick="btncandidaterequesthistorysearch" cssclass="btn btn-primary" causesvalidation="false" ><span class="glyphicon glyphicon-search"></span></asp:linkbutton> </span> </div> </div> <br/><br/> </contenttemplate> </asp:updatepanel>
code behind
protected void drpcrhistoryframework_selectedindexchanged(object sender, eventargs e) { frameworkid = convert.toint32(drpcrhistoryframework.selectedvalue); drpcrhskillname.datasource = competencymanager.getcompetencybyframeworkandmentoringrequests(((int)session[commonhelper.constants.current_candidate]), frameworkid); drpcrhskillname.databind(); drpcrhskillname.items.insert(0, new listitem("-- competency --", "0")); string script = @" $(function () { // trigger when document ready $('.datepicker').datepicker(); //initialise date pickers })"; scriptmanager.registerclientscriptblock((sender control), this.gettype(), "alert", script, true); //scriptmanager.registerstartupscript(updatepanelcrhistory, updatepanelcrhistory.gettype(), "alert",script, true); updatepanelcrhistory.update(); }
the issue set javascript called when page ready. when update using update panel page isn't loaded again, separate sections updated. these sections contain html needs javascript function initialize controls again.
you can trigger initialize controls both initial load updated update panels using following (typing head on macbook can't test it) adding bottom of master-page or @ bottom of specific page:
<script type="text/javascript"> function partialpostbackfinished(sender, args) { var updatepanels = args.get_panelsupdated(); (i = 0; < updatedpanels.length; i++) { //do whatever needs triggered each updated update panel initializecontrols(updatedpanels[i]); } } function initializecontrols(container) { $('.datepicker', container).datepicker(); //initialise date pickers } if (sys != undefined) { //there's scriptmanager on page var prm = sys.webforms.pagerequestmanager.getinstance(); prm.add_endrequest(partialpostbackfinished); if (!prm.get_isinasyncpostback()) { //trigger initialization synchronous post triggered outside updatepanels initializecontrols(document); } } else { //trigger initialization pages without scriptmanager initializecontrols(document); } </script>
this replace need registerclientscriptblock, remove code-behind.
Comments
Post a Comment