- Button Web Server Control
in ASP.NET, we are able to use Button for make any event. Web server controls include three kinds of buttons, each of which appears differently on Web pages.
Button types
- Button -Presents a standard command button, which is rendered as an HTML input element.
- LinkButton -Renders as a hyperlink in the page. However, it contains client-side script that causes the form to be posted back to the server.
- ImageButton -Allows you to specify a graphic as a button.
Button Validation
In case a page contains validator controls, then, clicking a button control causes the validator control to perform its check. If client-side validation is enabled for a validator control, the page is not submitted if a validation check has failed. The following table describes the properties supported by button controls that enable you to control the validation process more precisely.
the validation properties are,
- CausesValidation -Specifies whether clicking the button also performs a validation check. Set this property to false to prevent a validation check.
- ValidationGroup -Allows you to specify which validators on the page are called when the button is clicked.
- Add Client Script Events for Buttons
in ASP.NET web page,you are able to add client Script directly as one of the propery. Alternatively, you can add client script programmatically, which is useful if the event or the code relies on information that is available only at run time.
To add a client Script directly
- In the control, add an attribute for the event, for example, onClientclick or onkeyup.
- add the following code,the folling code snippets explains you
-
<html>
<head id="Head1" runat="server">
<title>Button Page</title>
<script type="text/javascript">
var previousColor;
function MakeRed()
{
previousColor = window.event.srcElement.style.color;
window.event.srcElement.style.color = "#FF0000";
}
function RestoreColor()
{
window.event.srcElement.style.color = previousColor;
}
</script>
</head>
<body>
<form id="form2" runat="server">
<asp:button id="Button1" runat="server"
text="Button1"
<b>onmouseover="MakeRed();"</b>
<b>onmouseout="RestoreColor();"</b> />
</form>
</body>
</html>
-
- To add a client Script programmatically
- In the page's Init or Load event, call the Add method of the control's Attributes collection.
- add the following code,the folling code snippets explains you
-
protected void Page_Load(object sender, EventArgs e)
{
String displayControlName = "spanCounter";
TextBox1.Attributes.Add("onkeyup", displayControlName +
".innerText=this.value.length;");
}
-
- Related Links
-
3c1119c0-b5cd-4e1a-b79b-30767e246a99|0|.0