Asp.net|client
The previous chapter has written about the use of the RegisterStartupScript method in ASP.net, and in the. NET Framework there are methods similar to this: RegisterClientScriptBlock. Both methods are used to write scripts backwards from the foreground, and the only difference between the two methods is to send a script block from "where". RegisterClientScriptBlock () sends a script block at the beginning of the Web form (immediately after
the identity.
RegisterStartupScript () is used to add a script block to run after the page is loaded, and the script block added by this method is at the end of the Web form because the HTML element that the script is to modify must be defined before the script runs. That is, if you want to use client script to set the focus to a text box, you must make sure that the HTML markup for the text box is before the script that sets the focus of the text box.
The RegisterClientScriptBlock () method is used for scripting code that is executed in response to a client event. The script block sent by this method is at the beginning of the Web page because it does not require that the script block be placed after all HTML elements, that is, it generally does not use a custom control within the document.
If you just register a few functions, the effect is the same. But if you want to register some global scripts, such as defining some global variables and assigning values, then its precedence in HTML may be more important, and you should use RegisterStartupScript to ensure that the script can be executed before.
The following is a usage instance of the method (see MSDN):
<script language= "VB" runat= "Server" >
Sub Page_Load (sender as object,e as EventArgs)
' Form the script ' is registered at client side.
Dim scriptstring as String = "<script language=javascript> function DoClick () {"
Scriptstring + = "myform.show.value= ' Welcome to Microsoft." NET '}< '
scriptstring = "/"
scriptstring = "Script>"
If (Not isclientscriptblockregistered ("ClientScript"))
RegisterClientScriptBlock ("ClientScript", scriptstring)
End If
End Sub
</script>
<body topmargin= "leftmargin=" >
<form id= "MyForm" runat= "Server" >
<input type= "text" id= "show" style= "width=200" >
<input type= "button" value= "ClickMe" >
</form>
</body>