Asp.net uses jquery to add javascript processing for server controls

Source: Internet
Author: User

Server controls are often used in web development using asp.net, but server controls are not as easy to add as html controls.
Javascript events, processing,
So how to add js events to the server control,
Now we can use the mature javascript framework: jquery
In jquery, the method for binding events to page elements is $ ("# ID "). bind ("click", function () {}), which binds the click event to the element whose id is ID.

We know that the id of the server control after html is generated remains unchanged, that is, the id specified for the server control will also be the id of the element that generates html.

Therefore, you can use jquery to bind events to them,
For example:
Bind an event to <asp: Button ID = "deleteInfo" runat = "server" Text = "delete" CssClass = "btn_2k3" OnClick = "deleteInfo_Click"/>
The Code is as follows:
$ (Document). ready (function (){
$ ("# DeleteInfo"). bind ("click", function (){
Return confirm ('Are you sure you want to delete it? ');
})
})

Another special case is that when the server control's Visible = "false", the generated html id is no longer consistent with the server control's id.
How can we solve this problem:
You can place the server control in a display = none layer, instead of setting the visible = false of the server control.
For example, <div style = "display: none">
<Asp: Button ID = "deleteInfo" runat = "server" Text = "delete"/>
</Div>
In this way, the server control is invisible and the id is not changed. We can also operate it using js.

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.