Command Name in Button Control

Posted by Murugavelmsc under C# on 2/11/2013 | Points: 10 | Views : 1619 | Status : [Member] | Replies : 6
Experts,


<asp:Button ID="Button5" runat="server" Text="Add" OnClick="Submit1" CommandName="ADD" />
<br />
<asp:Button ID="Button6" runat="server" Text="Update" OnClick="Submit1" CommandName="MODIFY" />
<br />
<asp:Button ID="Button7" runat="server" Text="Del" OnClick="Submit1" CommandName="DELETE" />


protected void Submit1(object sender, EventArgs e)
{
Button btn = (Button) sender;
string code = btn.CommandName;
switch (code)
{
case "ADD":
Response.Write("ADD COMMAND");
break;
case "MODIFY":
Response.Write("Upadte COMMAND");
break;
case "DELETE":
Response.Write("Delet COMMAND");
break;

}

it throw an following error

'Button' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)

Please help me.

Thanks,
Murugavel S

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/



Responses

Posted by: Kusumas on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

protected void Submit1(object sender, EventArgs e )
{
Button btn = (Button) sender;
string code = btn.CommandName;
switch (code)
{
//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
}

U can use CommandEventArgs instead of EventArgs and
in switch direct u can use like this
swich(e.CommandName)
{
//write your code.....................
case "ADD":
Response.Write("ADD COMMAND");
break;
case "MODIFY":
Response.Write("Upadte COMMAND");
break;
case "DELETE":
Response.Write("Delet COMMAND");
break;
}
Thanks


Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
hello
go to your button property, click on events menu option double click on Command your .aspx.cs page will open with this type of method
protected void btnsign_Command(object sender, CommandEventArgs e)

{

}

then write your code in this....like below
protected void btnsign_Command(object sender, CommandEventArgs e)

{
Button btn = (Button) sender;
string code = btn.CommandName;
switch (code)
{
case "ADD":
Response.Write("ADD COMMAND");
break;
case "MODIFY":
Response.Write("Upadte COMMAND");
break;
case "DELETE":
Response.Write("Delet COMMAND");
break;

}


Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Even I have changed the code as above, it throws an error

No overload for 'Submit1' matches delegate 'System.EventHandler'


Thanks,
Murugavel S


Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
have you followed my procedure.....actualy you have to change accordingly in design mode also like this OnCommand="Submit1_Command"

Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

When I am using OnCommand event ti works fine.

But I want the same functionality in OnClick event.

Thanks
Murugavel S

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
you have to cahnge something like this in ur .aspx page in button html
<asp:Button ID="Submit1" runat="server" OnClick="Submit1_Click" Text="sign in" OnCommand="Submit1_Command" />


Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response