[Getting started with ASP. NET] spokesperson-client Behavior

Source: Internet
Author: User


[Getting started with ASP. NET] Spokesperson
-- Client Behavior

 

The famous brand server Control (Control) joins hands with the Olympic champion client HTML tag as the image spokesperson. The new image is themed with "Fast experience", which not only interprets the powerful information processing capabilities of the server Control, it also shows that the client HTML Tag hopes to prove its determination and attitude with flexibility and kindness.
 
■ Internal and external-property and attribute


Recently, Google has released a unique name in addition to English-Google, where Chinese users are in full swing,
For the time being, no matter whether Google is more suitable than Hu Yi, but Google wants to describe the product connotation to please Chinese users. This year, Web 4 billion attracted countless venture capitalists to compete, with US $.
The Chinese mainland is full of money. As a result, the websites have changed their faces like Sichuan Opera and changed their faces into a blog, bookmarks, and circles. Calm down, data mining, user body
Verification has become the key to a website. Some people say that three points of appearance (content) and seven points of dressing (Content Aggregation and user experience ). That is to say, the human-computer interaction design is from dispensable to essential, and the Ajax technology becomes increasingly popular.
Strange.


Server-side processing is always the core function of the WEB application system, and most of the information processing is completed on the server. However, if you put all the functions on the server side, let the page carry hundreds of thousands of bytes
ViewState shuttles back and forth in the network, which is not disgusting. Therefore, we need the client to act as the server spokesperson and share some system functions by executing the script program, so that
Users are better, making the server easier.


ASP. NET interacts with users through controls, but as far as the client browser is concerned, it never heard of and does not deal with controls. It is familiar with HTML tags and client script programs. To allow the client to charge
When the server spokesperson is used, the two must communicate with each other, that is, adding client behavior, generating client scripts, and ing between control behavior and client behavior.


Attribute and property are an interesting synonym. In MSDN, there is a saying: "In the tag of ASP. NET Server controls, attributes can be used.
Attribute to Set Properties)
Value ." Set attributes? Dizzy. In simple terms, property is a member of the class, and attribute is the appearance of the class. The former is the characteristics of the brand, and the latter is the characteristics of the Spokesperson.
You can use the latter to show yourself. It is reflected in the control and the corresponding HTML Tag. The control status is property, and the corresponding string in the tag is attribute, that is, the server seen by the browser.
The shape of the end control.

■ Announcing-adding client Behavior

An HTTP Response stream is a string of response streams. An HtmlTextWriter instance is generated on the page to collect HTML markup text generated by controls in the order of the Control tree, let the RenderControl method of the control check its Visible attribute. If it is true, the Render method of the control is called to add the markup text to the instance. After this is done, the RenderChildren method is called to recursively pass down. Therefore, when the page calls the control Render method or before, you can dynamically modify the Attribute in the tag to assemble it into the corresponding tag text.

The simplest way to add a client behavior to a control is in. the tags in the apsx file directly declare the corresponding Attribute, or call the Add method of the Attributes set of the control programmatically, that is, to announce to the client browser that my spokesperson is Lao Xu ~~

<% @ Page Language = "C #" %>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e ){
// Add onmouseover and onmouseout client behavior for the btnTest Control
BtnTest. Attributes. Add ("onmouseover", "MakeRed ();");
BtnTest. Attributes. Add ("onmouseout", "RestoreColor ();");
}
</Script>
<Html> <Script language = "javascript">
Var previuscolor;
// Change the foreground color
Function MakeRed (){
Previuscolor = window. event. srcElement. style. color;
Window. event. srcElement. style. color = "# FF0000 ";
}
// Restore the original foreground
Function RestoreColor (){
Window. event. srcElement. style. color = previuscolor;
}
</Script>
</Head> <body> <form id = "form1" runat = "server">
<Asp: button id = "btnTest" text = "btnTest" runat = "server"/>
</Form> </body>


■ Injection-dynamically add client scripts

In addition to explicit declaration in the. aspx file, the Page class also provides some methods for dynamically creating client scripts. The so-called dynamic generation script is actually to generate the corresponding script string, and then inject it to the HtmlTextWriter instance using RegisterClientScriptBlock and RegisterOnSubmitStatement provided by the Page class. At the same time, ASP. NET also provides several methods to assist in script generation, which is nothing more than adding some hidden input fields, arrays and other things.

You can also add the required script string on the OnPreRender method call notification page. This is not implemented in the Render method because when the HtmlTextWriter instance is passed to the control, the Page class has generated the corresponding text in the HtmlTextWrite instance, which is a step slower. The following example shows how to add a confirmation dialog box for page sending back.

<% @ Page Language = "C #" %>
<Script runat = "server">
Protected void Page_Load (Object sender, EventArgs e ){
String scriptText = "return confirm ('Do You Want To submit? ')";
// Add the script executed when the page is submitted.
RegisterOnSubmitStatement ("ConfirmSubmit", scriptText );
}
</Script>
<Html> <Asp: button id = btnTest text = PostBack runat = "server"/>
</Form> </body>


■ Join hands-GetPostBackEventReference Method


Most of the time, we hope that the client behavior can be combined with the corresponding server behavior to complete a complete system function, so that the client behavior can trigger the corresponding server behavior, that is, the ing between the two. In
ASP. in NET1.1, the page is essentially an atomic sending unit. The client behavior can only write data with its own characteristics to the hidden flag domain before the page is sent back. After the page is sent back, control passed the check
The data in the hidden flag field is checked to trigger the corresponding behavior.

There are two typical examples for the client to work with the server: one is the GetPostBackEventReference method, which can lead to page return with feature data; the other is the well-known validator architecture. In addition, standard controls are also implemented through client behavior. The following uses the previous example to demonstrate the complete "generate-client processing-send-server processing" process.

Public class MyControl: Control, IPostBackEventHandler {
// Define the Number attribute
Public int Number {
Get {
If (ViewState ["Number"]! = Null)
Return (int) ViewState ["Number"];
Else
Return 50;
}
Set {ViewState ["Number"] = value ;}
}
// Event processing dependent on user operations. For details, refer to "suixiang 11".
// EventArgument is the event data, which is set by GetPostBackEventReference.
// Event data directly changes the control status
Public void RaisePostBackEvent (string eventArgument ){
If (eventArgument = "inc") Number ++;
If (eventArgument = "dec") Number --;
}
// Generate HTML text corresponding to the control
// In addition to generating the Number attribute text, two hyperlinks are also generated to trigger the sending and returning event parameters.
// The GetPostBackEventReference method sends out the client script to start the sending back and provides reference
Protected override void Render (HtmlTextWriter writer ){
Writer. Write ("Control Value:" + Number. ToString () + "");
Writer. Write ("<a href = \" javascript: "+
Page. GetPostBackEventReference (this, "inc") + "\"> + </a> ");
Writer. Write ("or ");
Writer. Write ("<a href = \" javascript: "+
Page. GetPostBackEventReference (this, "dec") + "\">-</a> ");
}
}

 

As ASP. NET1.1 is a page-based atomic re-release unit, the communication and connection between the Spokesperson and the brand are costly. To improve the performance, you must break the system that uses pages as the atomic sending unit.

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.