Test your technical skills online, appear in Virtual Interview and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Articles Author
Wed, 16-Apr-2014 Authors
All Time Authors
Sourav.Kayal
39750
SheoNarayan
34800
Niladri.biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

Button Tips and Tricks Part-I

Sheonarayan
Posted by under ASP.NET category on for Intermediate level | Points: 250 | Views : 4877 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
2 vote(s)

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.

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




About the Author

Sheonarayan

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Microsoft_MVP,Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan The Founder http://www.dotnetfunda.com
http://www.snarayan.com

Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan
Login to vote for this post.

Comments or Responses


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Comment using Facebook