Asp. NET extract--server-side control article (ii)

Source: Internet
Author: User
Tags implement interface client

Often see a similar problem on the Web: How do I get asp.net server-side controls to respond to client events?

Asp. NET Server-side controls can respond to server-side events, enabling us to write Web pages just as you would write a Windows program. But sometimes we do not need to let the client run the control and the server interaction, the overhead is very large: not only occupy network bandwidth, the server's CPU resources, but also produce a postback cause the client browser "refresh" affect the interface effect.
If you add client event code to a server-side control directly within the HTML code of the Web form, just like this:
<asp:button id= "Button1" text= "button 1"/>

You will find that when you press this button, there is no scheduled message box to pop up, but instead directly executes the Button1_Click method of the Response button in the server-side code to press the event (assuming we've bound this event for the button). If you look at the source code of the client page, the button's onclick event, there is no code written by itself, but it becomes "javascript:__dopostback (...)".

In fact, this is part of the. NET Framework transformation effort. With this "javascript:__dopostback (...)", you can implement a series of actions such as button submission, server-side response events, and so on. (for this automatically added script, I'll explain in detail in future articles.) )

If you want the control to respond to events on the client side, you must find another way. Fortunately. NET has provided us with a rich interface to implement these functions.

All server-side controls (Button, Label, TextBox ...). Has a property attributes--this is an important attribute, which is a collection of server-side controls that correspond to HTML element attributes and events on the client. With it, we are able to customize the behavior and appearance of the server-side controls on the client.

Or just Button1, this time we add such a sentence in the Page_Load incident (outside the IsPostBack judgment):
BUTTON1.ATTRIBUTES.ADD ("onclick", "Window.alert (' button is pressed ')");

After rebuilding the project and browsing the page again, we can finally see that the "button is pressed" message box is ejected normally.
Go back and explain the code: attributes is the Button1 property that is owned by all server-side controls, ADD (...) is a method that attributes belongs to to add code to the client's HTML markup for the control, the first parameter is the name of the property, and the second parameter is the value of the property. Here we add a pop-up message box code for the button's onclick event. In the Client view page source code, you can see the button's OnClick event contains this code (the. NET framework automatically generated "__doPostBack (...)") After that, we use Add to tell the. NET Framework: "Join our code First!" ”)。

Attributes also has another form of access (C # dedicated):
button1.attributes["onclick"] = "window.alert (' button was pressed ')"; [] is the name of the attribute, and the right of the equal sign is the value of the property

This method can also be used to change properties for a control using server-side code:
button1.attributes["style"] = "fontsize:9pt"; Change the font of the control style to 9 points






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.