The use of RegisterClientScriptBlock in asp.net

Source: Internet
Author: User
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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.