Delegation and events of ASP. NET Learning Series-3-adding client events on the server side

Source: Internet
Author: User

  Reading directory

I. Why do I need to add a client script?

Ii. ASP. NET Server controls can send two client scripts

3. Sending client scripts in ASP. NET

4. HTML attributes sent in ASP. NET

V. Instances

  I. Why do I need to add a client script?

    Although from a technical point of view ASP. all functions of the NET Server control can be executed on the server. We know that because the server control is executed on the server, every execution will have a round-trip, which will increase the pressure on the server, in some cases, we can also add client scripts. There is only one server, and there are countless browser clients. If we execute client scripts on countless browser clients, this will greatly reduce the pressure on the server, by adding client scripts, You can greatly enhance the availability of server controls, such as ASP. the. NET verification control can be verified on the server side. However, in a high-version browser, the verification control also sends client scripts for verification on the client side, therefore, not all server controls are executed on the server. The efficiency of using verification controls is not as low as you think.

  Ii. ASP. NET Server controls can send two client scripts

1. client script block: a client script block is a function written in javascript.

2: client HTML attribute: the method used to associate client events with client scripts, just like the "this. btnOK. attributes. add ("onclick", strScript );"

  3. Sending client scripts in ASP. NET

    You can use the System. Web. UI. Page class to send client scripts to the HTML provided by ASP. NETWeb pages.

 1: RegisterClientScriptBlock (key, script): the client script is sent at the beginning of the Web form, that is, the <form runat = "server"> identifier.

2: RegisterStartupScript (key, script): Send the client script before the Web form is identified.

  4. HTML attributes sent in ASP. NET

    The WebControl class includes the WebControl. Attributes. Add method to Add an HTML attribute or event to the HTML element generated by the Web server control.

  V. Instances

1: Default. aspx. cs code file

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Namespace AddClientEvent
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Send the HTML attributes of the Client
String strScript = @ "return confirm ('Are you sure you want to delete it? ');";
This. btnOK. Attributes. Add ("onclick", strScript );
If (! Page. IsPostBack &&! Page. IsStartupScriptRegistered ("Popup "))
{
// Send the client script block
String strScriptBlock = "<script> alert ('send client script') </script> ";
Page. RegisterStartupScript ("Popup", strScriptBlock );
}
}
}
}

Sending client HTML attributes

  

// Send the client script block

  

Check whether the client script is sent before the </form> identifier, because we use Page. RegisterStartupScript.

 

 

Related Article

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.