How do I call JS client script in UpdatePanel

  • 2020-05-09 19:08:15
  • OfStack

UpdatePanel, Response.Write (" < script > function dis (){alert(' this is JS' written in server); } < /script > ") to invoke the client script, no response without any prompt. How do I call the JS client script in UpdatePanel?

Methods: ScriptManager.RegisterStartupScript (Control controlId,Type this.GetType (),String key,String script block) method was used.
Some people say that controlId must be in UpdatePanel, but actually, page control will do.

A specific example is given below:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
ScriptManager.RegisterStartupScript(BtnJs, this.GetType(), "alert", "<script>function 
dis (){alert(' This is the call written at server the JS , such as the use Response.Write() Can not achieve this effect!!  ');}</script>", false); 

 
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" > 
</asp:ScriptManager> 
<input id="BtnJs" type="button" value="CallServerJs" onclick="dis()" runat="server"/> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Block"> 
<ContentTemplate> 
<asp:Button ID="Button6" runat="server" Text="Button" OnClientClick="dis()"/> 
</ContentTemplate> 
</asp:UpdatePanel> 

Note: BtnJs is the button outside UpdatePanel and Button6 reuses the server side registered script. Note: if you want to register the client side script in normal aspx, you can use it
Page.ClientScript.RegisterStartupScript (this.GetType (), String Key,String Js block,Bool AddScriptTag) Purple parts cannot be included, if written, the script will be automatically commented out when browsing!

Related articles: