最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Update Javascript after Async Post Back in ASP.NET - Stack Overflow

programmeradmin2浏览0评论

I have the following code on my website:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="myButton_btn" eventname="Click" />
    </Triggers>
        <ContentTemplate>
            <script>
                function pageLoad(){
                    window.alert("<%=Session("myVariable")%>"); 
                    }
                }
            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:Button ID="myButton_btn" Text="Prev Month" runat="server"></asp:Button>

The javascript is running everytime I click the button. However, even though my subroutine updates the session variable after clicking the button, I always get the same alert. In other words, the same javascript is run after postback, and it is not updated. I am positive that the session variable IS being changed. Does anyone know why this is happening? I appreciate your input!

Thank you so much!

I have the following code on my website:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="myButton_btn" eventname="Click" />
    </Triggers>
        <ContentTemplate>
            <script>
                function pageLoad(){
                    window.alert("<%=Session("myVariable")%>"); 
                    }
                }
            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:Button ID="myButton_btn" Text="Prev Month" runat="server"></asp:Button>

The javascript is running everytime I click the button. However, even though my subroutine updates the session variable after clicking the button, I always get the same alert. In other words, the same javascript is run after postback, and it is not updated. I am positive that the session variable IS being changed. Does anyone know why this is happening? I appreciate your input!

Thank you so much!

Share Improve this question edited Feb 4, 2011 at 17:00 Victor 4,7211 gold badge28 silver badges31 bronze badges asked Feb 4, 2011 at 16:41 SebolainsSebolains 7822 gold badges10 silver badges16 bronze badges 1
  • Have you tried to call UpdatePanel.Update() in the myButton_btn.Click-Handler? Therefore i would remove the explicit trigger and set ChildrenAsTriggers of panel to false. – Tim Schmelter Commented Feb 4, 2011 at 17:15
Add a ment  | 

2 Answers 2

Reset to default 5

Try this in the button-handler:

VB.Net

ScriptManager.RegisterStartupScript(Me.UpdatePanel, GetType(string), "alertScript", String.Format("alert('{0}');",Session("myVariable")), true)

C#

ScriptManager.RegisterStartupScript(this.UpdatePanel, typeof(string), "alertScript", string.Format("alert('{0}');", Session["myVariable"]), true);

I'm sure there's a better way to do this, but my hack for handling javascript returned within an updatepanel involves registering a function that looks for the new javascript within any UpdatePanels that were updated and then eval()'s their contents. Here's a rough example:

Site.Master:

<script type="text/javascript">
    PageLoadedHandler = function(sender, args) {
        var panels = args.get_panelsUpdated();
        for (var i = 0; i < panels.length; i++) {
            var scripts = panels[i].getElementsByTagName('script');
            for (var j = 0; j < scripts.length; j++) {
                eval(scripts[j].innerHTML);
            }
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoadedHandler);
</script>

I'm definitely open to cleaner solutions if anyone has one.

发布评论

评论列表(0)

  1. 暂无评论