The ScriptManager.RegisterStartupScript of method is invalid on the ajax page

  • 2020-05-09 18:26:49
  • OfStack

If Ajax is not used, cs can run a section of js code as follows:
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " < script > window.open('default2.aspx') < /script > ");
If Ajax is used in the page, the above code will not work even when executed. To cope with this situation, we usually use:
ScriptManager.RegisterStartupScript(this.Button1, this.GetType(), "alertScript", "window.open('default2.aspx');", true);
The first parameter is the control ID to register the script. Try 1, as long as it is on this page.
The second parameter is to register the script control type, whether it is a control or this's GetType () or typeOf(string).
The name of the third script function is arbitrary.
The fourth is the script content.
The fifth is to indicate whether to add the script tag, if the fourth parameter is included < script > < /script > Tag, false here, true otherwise.

Note: the aspx code looks like this

< div >
< asp:UpdatePanel ID="UpdatePanel1" runat="server" >
< ContentTemplate >
< asp:TextBox runat="server" ID="TextBox2" >
< /asp:TextBox >
< asp:Button runat="server" Text="Button" ID="Button1" nClick="Button1_Click" / >
< /ContentTemplate >
< Triggers >
< asp:PostBackTrigger ControlID="Button1" / >
< /Triggers >
< /asp:UpdatePanel >
< /div >

I registered the script in the Button1_Click event. I must add the red part of 1, otherwise it will always prompt that I can't parse anything!

In addition, js cannot interfere with the cs code. So once the script is registered, the js and cs code will run independently of each other.

Related articles: