September 13, 2016 21:14 by
Peter
Using Command event over Click events give us better control over Coding as it provide proper manageable way to control events in a single module.
If we tend to had 5-6 button the rather than using 5-6 button click events we can use "Command" event. Multiple buttons will have same Command Event function but Multiple buttons cannot have same Click button event. each Click and Command Button are often related to a single button but always Click event will be fired firstly. "CommandName" property of each button must also be specified for each button.This property will decide
"what to do" in Command function this is common for multiple buttons.
Using Command event over Click events provide us better control over coding as it provide proper manageable way to control events in a single module.
Note:
"CommandName" property is important and required of buttons if we are using "Command" event. we can rename the Command Event function's name.
Follow this pattern,
protected void Button_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName) {
case "button1":
Response.Write("Click from 1");
break;
case "button2":
Response.Write("Click from 2");
break;
case "button3":
Response.Write("Click from 3");
break;
}
}
and in aspx file,
<asp:Button ID="Button1" runat="server" Text="Button1" CommandName="button1" OnCommand="Button_Command" />
<asp:Button ID="Button2" runat="server" Text="Button2" CommandName="button2" OnCommand="Button_Command" />
<asp:Button ID="Button3" runat="server" Text="Button3" CommandName="button3" OnCommand="Button_Command" />
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.