Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,549 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,872 people online right now. Registration is fast and FREE... Join Now!




Button CommandName

 
Reply to this topicStart new topic

Button CommandName, Need to use identify which button was clicked to call a function and t

Elven
13 Aug, 2008 - 01:53 PM
Post #1

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 7

Hi,

I am using following code:
CODE

protected void SpremiPromjene(Object Sender, CommandEventArgs e)
switch (e.CommandName)
            {
                case "HomeCmd":
                        ///some commands
                 break:
            ///more cases
                   }

///ASP code

<asp:Button ID="HomeBtn" runat="server" Text="Spremi promjene" CommandName="HomeCmd" OnClick="SpremiPromjene" />


But I keep getting: Error 18 'System.EventArgs' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'System.EventArgs' could be found.

I am doing the code as they do it on MSDN web ( http://msdn.microsoft.com/en-us/library/sy...ommandname.aspx ), but for some reason it ain't enough.

Any help would be appreciated.
Thanks in advance.
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Button CommandName
13 Aug, 2008 - 04:07 PM
Post #2

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Is this asp.net code because if it is it should be in the ASP.NET section, but do not post the same topic in the ASP.NET section wait for a moderator to move it.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Button CommandName
13 Aug, 2008 - 04:21 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Try changing this line


HTML
<asp:Button ID="HomeBtn" runat="server" Text="Spremi promjene" CommandName="HomeCmd" OnClick="SpremiPromjene" />


to

HTML
<asp:Button ID="HomeBtn" runat="server" Text="Spremi promjene" CommandName="HomeCmd" OnCommand="SpremiPromjene" />


When you use OnClick you're invoking the OnClick Event, which is expecting EventArgs and you're passing it CommandEventArgs. Also, this is an ASP.NET topic so I'm moving it to the ASP.NET forum smile.gif
User is online!Profile CardPM
+Quote Post

Elven
RE: Button CommandName
14 Aug, 2008 - 12:05 AM
Post #4

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 7

Thanks for moving it to the correct section and even more thanks for solving this!

Mistake was definately there, worst part is that it is even stated that way in MSDN article I linked, but I just couldn't see it (that's what I get for not copy pasting the MSDN code wink2.gif ).

For people who might be interested in similar solutions, I found out as well that you could use
CODE
string strName = ((Control)sender).Name;

or
CODE
if (sender == name1) { ... }
  else if (sender == name2) { ... }
.

Didn't try those out, but might be helpful for people who will not want to complicate stuff with CommandName and OnCommand, but will get to this thread with a similar problem.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:08PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month