Adding JavaScript client events for server controls

Source: Internet
Author: User
Tags client
Server | client | Controls remember to use JavaScript frequently when using ASP. For example, verify the form, delete the confirmation, and so on. asp.net is server-side, our JavaScript has lost its application space. However, after some groping, in fact asp.net use JavaScript is very simple.


Let's first introduce the attributes properties of WebControl. (HTMLControl almost, no longer repeated), attributes is a collection of arbitrary attributes (rendering only) that do not correspond to the properties of the control. For example, a plain form control
<input type= "button" name= "button" value= "button" onclick= "Clickevent ()" >
The Type,name,button here are the properties of the button, the onclick button clicks after the event. These can be set by the attributes attribute set in asp.net.





Here's an example of verification of a login form.
<%@ Page language= "C #"%>
<script runat= "Server" >
void Page_Load (Object sender, EventArgs e)
{
Add Client onclick Event
button1.attributes["OnClick"]= "return Checkform ()";
}
</script>

<script language= "JavaScript" >
function Checkform ()
{
Verifying the Javascipt function of a form
if (document.all.username.value== "")
{
Alert ("Please fill in User name");
return false;
}
else if (document.all.password.value== "")
{
Alert ("Please fill in the password");
return false;
}
return true;
}
</script>
<body>
<form runat= "Server" >
<br/>
<table height= "Width=" >
<tbody>
<tr>
<td>
User name </td>
<td>
<asp:textbox id= "Username" runat= "Server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password </td>
<td>
<asp:textbox id= "PassWord" runat= "Server" textmode= "PassWord" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:button id= "Button1" runat= "server" text= "login" ></asp:Button>
</td>
</tr>
</tbody>
</table>
</form>
</body>

is not back to the familiar method:)



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.