Button Example in ASP.NET using C#

Button Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 20, 2010 at 6:22 PM Comments : [ 0 ]

In this article we will introduce with Button control in ASP.NET using C#.

Button Example in ASP.NET using C#

Button is rectangular in shape. Button Control in ASP.NET allow user to send a command. When the user click on Button the Button click event is fired and perform the operation. In this example we use onCommand event. You will set the CommandName property for each Button. CommandName is used in switch-case loop.  There are two button Submit and Cancel. When the user click on Submit Button the label shows the text.

Button.aspx (Design Page):

Button.aspx (source page)

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="Button.aspx.cs" Inherits="Button" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
Runat="Server">
<div>
<h2 style="color:Green">Button in ASP.NET 4 , C#</h2>
<asp:Button ID="button1"
runat="server" 
Text="Submit" 
CommandName="SubmitButtonCommand" 
OnCommand="Button_Command"/>
<br />
<br />
<asp:Button ID="button2" 
runat="server" Text="Cancle" 
CommandName="CancelButtonCommand" 
OnCommand="Button_Command"/>
<br />
<br />
<asp:Label ID="label1" runat="server" Font-Bold="True" ForeColor="#000099" />
</div>
</asp:Content>

Button.aspx.cs code (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Button : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button_Command(object sender, 
System.Web.UI.WebControls.CommandEventArgs e)
{
switch (e.CommandName)
{
case "SubmitButtonCommand":
label1.Text = "Submit Button Clicked";
break;
case "CancelButtonCommand":
label1.Text = "Cancel Button Clicked";
break;
}
}
}

Output:

The output is as follows.

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics