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.