ScriptBlock Control Overview

The ScriptBlock Control  enables you to control where, and how, JavaScript renders on a form. The control includes several properties that might affect the rendering.

The control utilizes the methods defined on ClientScriptManager and ScriptManager objects.

Default: To render JavaScript inline (no properties set)

This renders the JavaScript inline each time the page loads.

<%@ Control %>

Simple:

<sq:ScriptBlock runat="server">
  <script type="text/javascript">
                alert(1);
  </script>
</sq:ScriptBlock>


Force JavaScript registration

 This registers the JavaScript by using ClientScriptManager.

<sq:ScriptBlock runat="server" RegisterWithScriptManager="true">
  <script type="text/javascript">
                alert(1);
  </script>
</sq:ScriptBlock>

RegisterClientScriptBlock

It renders the JavaScript after the <form> tag (higher up on the page).

The following ASP.NET method is invoked: ClientScriptManager.

RegisterClientScriptBlock

For more information, read this article.


Control how many times the JavaScript is rendered on the page

 This renders the JavaScript only once, even though it appears multiple times. For example, you can use this option if the same JavaScript appears in several sub views, and you want to render it only once

<sq:ScriptBlock runat="server" RegisterWithScriptManager="true" RegistrationKey="Key1">
  <script type="text/javascript">
                alert(1);
  </script>
<sq:ScriptBlock runat="server" RegisterWithScriptManager="true" RegistrationKey="Key1">
  <script type="text/javascript">
                alert(1);
  </script>

The following ASP.NET method is invoked: ClientScriptManager.

RegisterClientScriptBlock

For more information, read this article.


Execute the JavaScript when the page finishes loading:

 <sq:ScriptBlock runat="server" RegisterWithScriptManager="true" RegistrationMode="Startup">
   <script type="text/javascript">
                 alert(1);
   </script>
 </sq:ScriptBlock>

 The following ASP.NET method is invoked: ClientScriptManager.

RegisterStartupScript

For more information, read this article.

 

Run the JavaScript on the initial load, and on every asynchronous postback when the UpdatePanels are updated

<sq:ScriptBlock runat="server" RegisterWithScriptManager="true" EnablePartialRendering="true">
  <script type="text/javascript">
                alert(1);
  </script>
</sq:ScriptBlock>

The following ASP.NET method is invoked: ScriptManager.

RegisterClientScriptBlock

For more information, read this article.

 

Run the JavaScript on the initial load when the page finishes loading, and on every asynchronous postback when the UpdatePanels are updated

<sq:ScriptBlock runat="server" RegisterWithScriptManager="true" EnablePartialRendering="true" RegistrationMode="Startup">
  <script type="text/javascript">
                alert(1);
  </script>
</sq:ScriptBlock>

 The following ASP.NET method is invoked: ScriptManager.

RegisterStartupScript

For more information, read this article.

  

Run the JavaScript on the initial load when the page finishes loading, and when UpdatePanel1 is updated

Here, the JavaScript is executed on the initial load when you click the Send1 button (but not when you click the Send2 button).

 <sq:ScriptBlock runat="server" RegisterWithScriptManager="true" EnablePartialRendering="true" UpdatePanelID="UpdatePanel1">
   <script type="text/javascript">
                 alert(1);
   </script>
 </sq:ScriptBlock>
 <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
                 <ContentTemplate>
     <%= { DateTime.Now.ToString() } %>
    <sq:Button runat="server" Text="Send1" />
   </ContentTemplate>
</asp:UpdatePanel>
 <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
                 <ContentTemplate>
     <%= { DateTime.Now.ToString() } %>
    <sq:Button runat="server" Text="Send2" />
   </ContentTemplate>
</asp:UpdatePanel>

 

The following ASP.NET method is invoked: ScriptManager.

RegisterClientScriptBlock

For more information, read this article.

About this Article
  • Created: 06/07/2017 3:19 am EDT
  • Last updated: 06/26/2017 4:42 am EDT
In This Article