-->

28 February 2020

ASP.NET Button Control

  Asp.Net CS By Example       28 February 2020

ASP.NET Button Control

 The Button control renders a push button that we can use to submit a form to the server. For example, the below code contains a Button control. When we click the Button control, the time displayed by a Label control is updated.

The Button control supports the following properties (this is not a complete list):

Sr.No Properties Description
1) AccessKey Specify a key that navigates to the Button control.
2) CommandArgument specify a command argument passed to the Command event.
3) CommandName Specify a command name passed to the Command event.
4) Enabled Enable or disable the Button control.
5) OnClientClick Specify a client-side script that executes when the button is clicked.
6) PostBackUrl Post a form to a particular page.
7) TabIndex Specify the tab order of the Button control.
8) Text Set or get text of the Button control.
9) UseSubmitBehavior Use JavaScript to post a form.

The Button control also supports the following method:

Sr.No Method Description
1) Focus Set the initial form focus to the Button control.

The Button control also supports the following two events:

Sr.No Event Description
1) Click Raised when the Button control is clicked.
2) Command Raised when the Button control is clicked. The CommandName and CommandArgument are passed to this event.

FrmPushButton.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmPushButton.aspx.cs"
    Inherits="LearnAsp.Net.ControlDemo.Button.FrmPushButton" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Button Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
        <br />
        <br />
        <asp:Label ID="lblTime" runat="server" />
    </div>
    </form>
</body>
</html>

FrmPushButton.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LearnAsp.Net.ControlDemo.Button
{
    public partial class FrmPushButton : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("T");
        }
    }
} 

  Output:
logoblog

Thanks for reading ASP.NET Button Control

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.