简体   繁体   中英

Async postback not working

I'm using this gauge in my contentplaceholder.

http://www.dariancabot.com/projects/jgauge_wip/

MyControl.ascx:

<link rel="stylesheet" href="Scripts/jgauge.css" type="text/css" />
<script type="text/javascript" src="Scripts/jquery-1.8.0.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/jgauge-0.3.0.a3.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/jQueryRotate.2.2.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/excanvas.min.js"></script>

<div id="<%=this.ClientID%>_ctl" class="jgauge" ></div>

<script type="text/javascript">
    $(document).ready(function () {
    var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
        if(isPostBack == "true")
        {
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
            prm.add_endRequest(onEndRequest);
        }
        else{
                var <%=this.ClientID%>_ctl;

                <%=this.ClientID%>_ctl = new jGauge(); 
                <%=this.ClientID%>_ctl.id = '<%=this.ClientID%>_ctl';
                <%=this.ClientID%>_ctl.init(); 
 }
    });

    function EndRequestHandler(sender, args){  
        var <%=this.ClientID%>_ctl;
        <%=this.ClientID%>_ctl = new jGauge(); 
        <%=this.ClientID%>_ctl.id = '<%=this.ClientID%>_ctl';
        <%=this.ClientID%>_ctl.init();
}
</script>

In MyPage.aspx: (Contains dynamically created table having several such controls. putting the generated table on placeholder)

<asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder id="phMain" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="myBtn" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="myBtn" runat="server" Text="Refresh" /> 

I have script manager on master page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>

But after async postback (hitting 'myBtn'), gauge disappears. Please Help. Trying to solve it since couple of days.

I was able to solve this with by going:

$(document).ready(function () { 
    Sys.WebForms.PageRequestManager.getInstance()
        .add_endRequest(<%=this.ClientID%>_ctlEndRequestHandler); 
    var <%=this.ClientID%>_ctl; 

    <%=this.ClientID%>_ctl = new jGauge();  
    <%=this.ClientID%>_ctl.id = '<%=this.ClientID%>_ctl'; 
    <%=this.ClientID%>_ctl.init();
});

function <%=this.ClientID%>_ctlEndRequestHandler(sender, args){
    var <%=this.ClientID%>_ctl; 
    <%=this.ClientID%>_ctl = new jGauge();  
    <%=this.ClientID%>_ctl.id = '<%=this.ClientID%>_ctl'; 
    <%=this.ClientID%>_ctl.init(); 
}

The only real difference is that the postback checks are no longer taking place. Your underlying problem is that $(document).ready will not fire on partial postbacks, which means that isPostBack is never actually getting set to true. Because of this, Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); was never executing, meaning that your EndRequestHandler never ran.

Edit

The other issue is that naming the method EndRequestHandler is guaranteed to cause problems if you have more than one of the control at the same time. To get around this, I appended <%=this.ClientID%>_ctl to the name of EndRequestHandler to ensure it was unique.

More info:

How do I use a jQuery $(document).ready and an ASP.NET UpdatePanel together?

http://encosia.com/document-ready-and-pageload-are-not-the-same/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM