ASP.net Passing Parameters in Button Click EventHandler

The Parameters can be passed by ASP.Net buttons by using CommandName and CommandArgument. Below is the code that demonstrates that how to pass Arguments using Asp.Net button in its event Handler. Just Specify CommandName and CommandArgument in the button and set the OnClick Event to your specified function.

public void ButtonEventHandler(Object sender, EventArgs e)
{
//Cast sender to Button
Button oButton = (Button)sender;
//Check for the appropriate arguments

switch (oButton.CommandName)
{
case "SelectData":
//Below would be your Logic to SelectData
SelectData(oButton.CommandArgument.ToString());
break;
case "SaveData":
//Below would be your Logic to SaveData
SaveData(oButton.CommandArgument.ToString());

break;
case "DeleteData":
//Below would be your Logic to DeleteData

DeleteData(oButton.CommandArgument.ToString());
break;

}
}

Its very Simple by this way you can Pass parameters using button and a single function would be able to handle different Buttons EventHandlers.

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies

Trouble In the House of Google