Can I get the ID or CommandName of a Server Control from my code behind code?

Discussion in 'ASP .Net Web Controls' started by Mike, Jan 23, 2004.

  1. Mike

    Mike Guest

    Hi,

    I have question that I think should I know the answer to but don't.

    In my ASP.NET file, default.aspx I have the following code.

    <asp:Button id="Mday1" OnClick="Mday_Click" Text="+" Runat="server"
    CommandName="Mday1"></asp:Button>
    <asp:Button id="Mday2" OnClick="Mday_Click" Text="+" Runat="server"
    CommandName="Mday2"></asp:Button>

    in my code behind file, default.aspx.cs I have the following function

    protected void Mday_Click(object sender, System.EventArgs e)
    {
    string debug = null, cname=null;

    debug = "You clicked control" + cname;
    }

    When I click either button on the web page it finds the event, (By
    setting a breakpoint I know it has). What I want to know is which of
    the buttons it came from. Is this possible or am I asking the wrong
    question?

    When I am debugging (in Visual Studio) by looking 'into' the sender ->
    System.Web.UI.WebControls.Button -> CommandName I can see what the
    command name is. How do I get this CommandName?

    Any help greatly appreciated.
    Mike
     
    Mike, Jan 23, 2004
    #1
    1. Advertisements

  2. hi mike,
    protected void Mday_Click(object sender, System.EventArgs e)
    {
    this.Response.Write("You clicked the " + e.CommandName + " button.");
    }
    "Mike" <> wrote in message
    news:...
    > Hi,
    >
    > I have question that I think should I know the answer to but don't.
    >
    > In my ASP.NET file, default.aspx I have the following code.
    >
    > <asp:Button id="Mday1" OnClick="Mday_Click" Text="+" Runat="server"
    > CommandName="Mday1"></asp:Button>
    > <asp:Button id="Mday2" OnClick="Mday_Click" Text="+" Runat="server"
    > CommandName="Mday2"></asp:Button>
    >
    > in my code behind file, default.aspx.cs I have the following function
    >
    > protected void Mday_Click(object sender, System.EventArgs e)
    > {
    > string debug = null, cname=null;
    >
    > debug = "You clicked control" + cname;
    > }
    >
    > When I click either button on the web page it finds the event, (By
    > setting a breakpoint I know it has). What I want to know is which of
    > the buttons it came from. Is this possible or am I asking the wrong
    > question?
    >
    > When I am debugging (in Visual Studio) by looking 'into' the sender ->
    > System.Web.UI.WebControls.Button -> CommandName I can see what the
    > command name is. How do I get this CommandName?
    >
    > Any help greatly appreciated.
    > Mike
     
    Alessandro Zifiglio, Jan 23, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    826
    Alphonse Giambrone
    Jun 28, 2003
  2. John Blair

    asp:linkbutton - commandname behaviour

    John Blair, Dec 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,112
    John Blair
    Dec 30, 2004
  3. Andrew Fisher
    Replies:
    4
    Views:
    517
    =?ISO-8859-1?Q?Anders_Nor=E5s?=
    Jan 31, 2005
  4. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    943
  5. Irfi
    Replies:
    0
    Views:
    709
  6. Robert Dodier
    Replies:
    6
    Views:
    2,617
    Matthias Kraft
    Sep 14, 2004
  7. UJ
    Replies:
    0
    Views:
    459
  8. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    718
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...