Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4093 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Button Tips and Tricks Part-I

Button Tips and Tricks Part-I

2 vote(s)
Rating: 5 out of 5
Article posted by Sheonarayan on 7/11/2011 | Views: 3813 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag


In this article we shall learn how to fire Server Side method on click of the button, how to access the CommandName property of the asp: Button (using OnCommand event) and what is the use of it and how to access the CommandName property of the asp:Button (using OnClick event) and what is the use of it.

Introduction

Button control is usually used to submit the HTML form (in this case we can say asp.net) to the server. Apart from this we can also perform some client side operations on click of the button.

I have written hundreds of ASP.NET, jQuery, HTML, CSS and JavaScript How to Tips and Tricks, click here to get them all.

How to fire Server Side method on click of the button?

In case we want to fire a server side method on click of a button, we can use this approach. 

To fire the server side event on click of the button, we need to specify a method name on OnClick event of the button and then declare that method in the code behind.

ASPX PAGE

<p>Enter your name:<asp:TextBox ID="txtBox1" runat="server" /></p>

<p><asp:Button ID="btnSubmit" runat="server" Text="Submit"

OnClick="SubmitData" /></p>

 

CODE BEHIND

 

protected void SubmitData(object sender, EventArgs e)

{

Response.Write("Your name is : " + txtBox1.Text);

}

OUTPUT

In the above code snippet, on click of the “btnSubmit”, the SubmitData method fires that write the text entered into the textbox on to the page by suffixing to “Your name is “.

How to access the CommandName property of the asp:Button (using OnCommand event) and what is the use of it?

In case we want to execute a single server side method on click of multiple buttons and want to differentiate which button was clicked, we can use this approach.

CommandName property can be used to determine which button is clicked in case we have multiple buttons on the page and firing the same method.

ASPX PAGE

<p><asp:Button ID="Button1" runat="server" Text="Insert" CommandName="Add" OnCommand="SubmitData" /></p>

<p><asp:Button ID="Button2" runat="server" Text="Update"

CommandName="Modify" OnCommand="SubmitData" /></p>

<p><asp:Button ID="Button3" runat="server" Text="Delete"

CommandName="Remove" OnCommand="SubmitData" /></p>

 

CODE BEHIND

protected void SubmitData(object sender, CommandEventArgs e)

{

string commandName = e.CommandName;

switch (commandName)

{

case "Add":

Response.Write("write insert code");

break;

case "Modify":

Response.Write("write update code");

break;

case "Remove":

Response.Write("write delete code");

break;

}

}

 

In the above code snippet, we have three buttons with same OnCommand event but different CommandName. To identify which button is clicked, we can access the CommandName property of theCommandEventArgs object and determine what to do. In this case, on click of Insert button, “write insert code” message is displayed and on click on Update button, “write update code” message is displayed and so on.

OnCommand is generally used in case of event bubbling (button is kept inside another control like GridView, DataList etc.).

How to access the CommandName property of the asp:Button (using OnClick event) and what is the use of it?

In case we want to specify a single server side method to execute on click on more than one button and want to determine which button was clicked, we can use this approach.

ASPX PAGE

<div>

<p><asp:Button ID="Button1" runat="server" Text="Insert" CommandName="Add" OnClick="SubmitData" /></p>

<p><asp:Button ID="Button2" runat="server" Text="Update" CommandName="Modify" OnClick="SubmitData" /></p>

<p><asp:Button ID="Button3" runat="server" Text="Delete" CommandName="Remove" OnClick="SubmitData" /></p>

</div>

 

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{

Button btn = (Button) sender;

string commandName = btn.CommandName;

switch (commandName)

{

case "Add":

Response.Write("write insert code");

break;

case "Modify":

Response.Write("write update code");

break;

case "Remove":

Response.Write("write delete code");

break;

}

}

 

In the above code snippet, we have three buttons with same OnClick event but different CommandName. To identify which button is clicked, we can type cast the sender object to the button (that gives access of the button control that fires the event) and access the CommandName property. In this case, on click of Insert button, “write insert code” message is displayed and on click on Update button, “write update code” message is displayed and so on.

Thanks for reading ! If you like this simple article subscribe to the RSS feed to get the new article alert directly in your inbox.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

Quick and dirty 3-Tier Architecture in ASP.NET with C#/VB.NET. Using UI BLL and DAL

Transforming the report output into PDF is an easy task now.

This Article explains on how to create a login page using C# and will be helpful for the beginners learning ASP.NET

To select multiple records from the GridView and persist the selection across different pages, we can follow this approach.

In this article i will show you how to use sitemap navigational control in asp.net

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 11:21:24 PM