To resolve this problem, add all the client-side script
blocks to a
StringBuilder method, and then register the
StringBuilder as one script block. To do so, follow these steps:
- In Microsoft Visual Studio .NET, use Microsoft Visual Basic
.NET or Microsoft Visual C# .NET to create a new ASP.NET Web application
project. Name the project RegJScriptBlock. By
default, WebForm1.aspx is created.
- Right-click WebForm1, and then click View
HTML.
- Replace the existing code with the following
code:
Visual C# .NET Code
<%@ Page language="c#" %>
<HTML>
<HEAD>
<script language="C#" runat="server">
public void Page_Load(object sender, System.EventArgs e)
{
int i = 0;
string scriptKey = "";
if(Page.IsPostBack.Equals(false))
{
ViewState["n"]=0;
}
else
{
ViewState["n"] = Int32.Parse(ViewState["n"].ToString())+1;
i = Int32.Parse(ViewState["n"].ToString());
}
string[] nubmerOfScripts = new string[Int32.Parse(ViewState["n"].ToString())+1] ;
StringBuilder sb = new StringBuilder();
for(int j=1;j<=i+1;j++)
{
sb.Append("<script language=JavaScript>");
sb.Append("if("+j+">1){clicked="+j+";} else {clicked=1;}");
sb.Append("function DoClick"+j+"() {");
sb.Append("myForm.textCount.value=clicked} <");
sb.Append("/");
sb.Append("script>");
sb.Append("\n");
nubmerOfScripts[j-1]=sb.ToString();
scriptKey = "clientScript"+j;
}
// Register all Java script as a Single Block
if(!this.IsClientScriptBlockRegistered(scriptKey))
this.RegisterClientScriptBlock(scriptKey, nubmerOfScripts[i]);
}
</script>
</HEAD>
<body onload="DoClick1()">
<form id="myForm" method="post" runat="server">
Serial Number of the Last JavaScript Registered:<INPUT id="textCount" type="text"><br>
<asp:Button id="AddScripts" runat="server" Text="Register JavaScript Block" Height="70px" Width="326px"></asp:Button>
</form>
</body>
</HTML>
Visual Basic .NET Code
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<script language= "vb" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Integer
Dim scriptKey As String
If (Page.IsPostBack.Equals(False)) Then
ViewState("n") = 0
Else
ViewState("n") = Int32.Parse(ViewState("n").ToString()) + 1
i = Int32.Parse(ViewState("n").ToString())
End If
Dim nubmerOfScripts As String()
ReDim nubmerOfScripts(Int32.Parse(ViewState("n").ToString()))
Dim j As Integer
Dim sb As New StringBuilder()
For j = 1 To i + 1 Step 1
sb.Append("<script language=JavaScript>")
If j > 1 Then
sb.Append("clicked=" + j.ToString() + ";")
else
sb.Append("clicked=1;")
End If
sb.Append("function DoClick" + j.ToString() + "() {")
sb.Append("myForm.textCount.value=clicked} <")
sb.Append("/")
sb.Append("script>")
sb.Append(Chr(13))
nubmerOfScripts(j - 1) = sb.ToString()
scriptKey = "clientScript" + j.ToString()
Next
' Register all JavaScript as a Single Block
If (Not (Page.IsClientScriptBlockRegistered(scriptKey))) Then
Page.RegisterClientScriptBlock(scriptKey, nubmerOfScripts(i))
End If
End Sub
</script>
</HEAD>
<body onload="DoClick1()">
<form id="myForm" method="post" runat="server">
Serial Number of the Last JavaScript Block Registered:<INPUT id="textCount" type="text"><br>
<asp:Button id="AddScripts" runat="server" Text="Register JavaScript Block" Height="70px" Width="326px"></asp:Button>
</form>
</body>
</HTML>
- On the Debug menu, click
Start to run the application.
- Click Register JavaScript Block to
register the new JScript blocks. Each time that the JScript block is
registered, the textbox displays the serial number of the last script block
that is added to the Web page.
- To register more than eight JScript blocks, repeat step
5 in this section of the article.
- View the source on the Web page to see the order where the
client script blocks are added. To do so, locate the following Web page and
then right-click View Source:
http://localhost/RegJScriptBlock/WebForm1.aspx
You can see that the JScript blocks are in sequence even when
the number of registered scripts is greater than eight.