Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: How do I programmatically determine which button was clicked?


Message #1 by gwade@w... on Fri, 11 Jan 2002 20:06:09
I'm reading Chapter 7 (Event-driven Programming and Postback) in Beginning 

ASP.NET USING C# regarding the ASP.NET Button Server control. I want to 

put multiple command buttons on a web form and then programmatically 

determine which button was clicked in the event handler. I saw some code 

at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-

us/cpgenref/html/cpconbuttonwebcontrol.asp) that does this 

but I can't get it to work. I receive the following error:



CS0123: Method 'ASP.ticTacToe_aspx.CommandBtn_Click(object, 

System.Web.UI.WebControls.CommandEventArgs)' does not match 

delegate 'void System.EventHandler(object, System.EventArgs)'



My version of this code is:



(Note: For now, I am only trying to get this code to work 

with one button)



<script Language="c#" runat="server">

void CommandBtn_Click(object sender, CommandEventArgs e)

{

   message.Text = "You clicked the " + e.CommandName + " - " + 

e.CommandArgument + " button.";

	 

}

</script>

<html>

  <head>

    <title>TicTacToe</title>

  </head>

  <body>

    <form runat="server">

      	<asp:Button id="SortAscendingButton" 

		Text="Sort Ascending"

		CommandName="Sort"

		CommandArgument="Ascending" 

		OnClick="CommandBtn_Click"

		runat="server"  />

      <br /><br />

	<asp:label id="message" runat="server" />

    </form>

  </body>

</html>



Does anyone have any suggestions? I understand that my event 

is being called with the wrong parameter type (i.e. EventArgs rather than 

CommandEventArgs. My question is how do I get this to work. 

Or is there another way of doing this. I can't seem to find anyone else on 

the Internet who has had this problem. Thank you.
Message #2 by "Albert Davis" <albertdavis@h...> on Fri, 11 Jan 2002 15:12:19 -0500
The error that you are receiving is telling you that your delegate has the 

wrong signature to what the System.Web.UI.WebControls.Button Class needs for 

you signin up for it's multicasted Click event...  You need to be using 

EventArgs not CommandEventArgs...



Al





>From: gwade@w...

>Reply-To: "ASP+" <aspx@p...>

>To: "ASP+" <aspx@p...>

>Subject: [aspx] How do I programmatically determine which button was 

>clicked?

>Date: Fri, 11 Jan 2002 20:06:09

>

>I'm reading Chapter 7 (Event-driven Programming and Postback) in Beginning

>ASP.NET USING C# regarding the ASP.NET Button Server control. I want to

>put multiple command buttons on a web form and then programmatically

>determine which button was clicked in the event handler. I saw some code

>at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-

>us/cpgenref/html/cpconbuttonwebcontrol.asp) that does this

>but I can't get it to work. I receive the following error:

>

>CS0123: Method 'ASP.ticTacToe_aspx.CommandBtn_Click(object,

>System.Web.UI.WebControls.CommandEventArgs)' does not match

>delegate 'void System.EventHandler(object, System.EventArgs)'

>

>My version of this code is:

>

>(Note: For now, I am only trying to get this code to work

>with one button)

>

><script Language="c#" runat="server">

>void CommandBtn_Click(object sender, CommandEventArgs e)

>{

>    message.Text = "You clicked the " + e.CommandName + " - " +

>e.CommandArgument + " button.";

>

>}

></script>

><html>

>   <head>

>     <title>TicTacToe</title>

>   </head>

>   <body>

>     <form runat="server">

>       	<asp:Button id="SortAscendingButton"

>		Text="Sort Ascending"

>		CommandName="Sort"

>		CommandArgument="Ascending"

>		OnClick="CommandBtn_Click"

>		runat="server"  />

>       <br /><br />

>	<asp:label id="message" runat="server" />

>     </form>

>   </body>

></html>

>

>Does anyone have any suggestions? I understand that my event

>is being called with the wrong parameter type (i.e. EventArgs rather than

>CommandEventArgs. My question is how do I get this to work.

>Or is there another way of doing this. I can't seem to find anyone else on

>the Internet who has had this problem. Thank you.












_________________________________________________________________

Chat with friends online, try MSN Messenger: http://messenger.msn.com



Message #3 by "Albert Davis" <albertdavis@h...> on Fri, 11 Jan 2002 15:17:05 -0500
Or you could change the eventtype on your webcontrol from "OnClick" to 

"OnCommand" and it should work as well...  Click and Command both get fired 

when the button is clicked...





>From: gwade@w...

>Reply-To: "ASP+" <aspx@p...>

>To: "ASP+" <aspx@p...>

>Subject: [aspx] How do I programmatically determine which button was 

>clicked?

>Date: Fri, 11 Jan 2002 20:06:09

>

>I'm reading Chapter 7 (Event-driven Programming and Postback) in Beginning

>ASP.NET USING C# regarding the ASP.NET Button Server control. I want to

>put multiple command buttons on a web form and then programmatically

>determine which button was clicked in the event handler. I saw some code

>at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-

>us/cpgenref/html/cpconbuttonwebcontrol.asp) that does this

>but I can't get it to work. I receive the following error:

>

>CS0123: Method 'ASP.ticTacToe_aspx.CommandBtn_Click(object,

>System.Web.UI.WebControls.CommandEventArgs)' does not match

>delegate 'void System.EventHandler(object, System.EventArgs)'

>

>My version of this code is:

>

>(Note: For now, I am only trying to get this code to work

>with one button)

>

><script Language="c#" runat="server">

>void CommandBtn_Click(object sender, CommandEventArgs e)

>{

>    message.Text = "You clicked the " + e.CommandName + " - " +

>e.CommandArgument + " button.";

>

>}

></script>

><html>

>   <head>

>     <title>TicTacToe</title>

>   </head>

>   <body>

>     <form runat="server">

>       	<asp:Button id="SortAscendingButton"

>		Text="Sort Ascending"

>		CommandName="Sort"

>		CommandArgument="Ascending"

>		OnClick="CommandBtn_Click"

>		runat="server"  />

>       <br /><br />

>	<asp:label id="message" runat="server" />

>     </form>

>   </body>

></html>

>

>Does anyone have any suggestions? I understand that my event

>is being called with the wrong parameter type (i.e. EventArgs rather than

>CommandEventArgs. My question is how do I get this to work.

>Or is there another way of doing this. I can't seem to find anyone else on

>the Internet who has had this problem. Thank you.












_________________________________________________________________

Chat with friends online, try MSN Messenger: http://messenger.msn.com



Message #4 by gwade@w... on Fri, 11 Jan 2002 20:49:55
Thanks Al.



 Changing    

     OnClick="CommandBtn_Click"

 to

     OnCommand="CommandBtn_Click" did the trick. 



 Now I can programmatically determine what was clicked. Incidentally I 

rechecked the documentation at MSDN (url above) and it uses OnClick. 

Thanks again!


  Return to Index