Introduction to Button Control:
Button: It displays text to click on and create a postback.1. Create a new web form.
2. Drag a button from toolbox on your page.
Properties:
a. ID: Set its ID.b. AutoPostBack: Button is a control which has its AutoPostBack property set to true by default.
c. OnClientClick: You can write any JavaScript function here that will be called before postback.
d. Text: You can set its text to a suitable name.
d. You can its style properties like CssClass, BackColor, Font-Bold, ForeColor, Height, Width etc.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.btnClass {
}
</style>
</head>
<body>
<form id="form1" runat="server" >
<div align="center">
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="btn" runat="server" Text="CLICK ME!" CssClass="btnClass"
BackColor="YellowGreen" Font-Bold="true" ForeColor="White"
OnClick="btn_Click" Height="50px" Width="200px" />
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.btnClass {
}
</style>
</head>
<body>
<form id="form1" runat="server" >
<div align="center">
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="btn" runat="server" Text="CLICK ME!" CssClass="btnClass"
BackColor="YellowGreen" Font-Bold="true" ForeColor="White"
OnClick="btn_Click" Height="50px" Width="200px" />
</div>
</form>
</body>
</html>
Functions:
Most common function of button is click, generate an onclick function by double clicking the button in design view.Now in the function generated in .aspx.cs file you can program your logic.
Response.Write allow us to write something on top of page from backend.
You can call other controls in this function and can save data.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
Response.Write("<div align='center'><br /><br /><br /><br /><br />You click a button</div>");
}
{
}
protected void btn_Click(object sender, EventArgs e)
{
Response.Write("<div align='center'><br /><br /><br /><br /><br />You click a button</div>");
}
You can make this button default button (A button which is pressed whenever user press enters on that page) by adding its id in form tag.
Done!
Run the program.
Click the button and you will get a message.
Let me know in the comment section if you have any question.
Previous Post:
TextBox control in asp.net C# Web Form
Next Post: