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. Advertising

  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. Advertising

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. John Blair

    asp:linkbutton - commandname behaviour

    John Blair, Dec 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,933
    John Blair
    Dec 30, 2004
  2. Irfi
    Replies:
    0
    Views:
    610
  3. Irfi
    Replies:
    0
    Views:
    421
  4. =?Utf-8?B?RGFiYmxlcg==?=

    use Formview cancel commandname to return to GridView Page?

    =?Utf-8?B?RGFiYmxlcg==?=, Mar 20, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    5,154
    =?Utf-8?B?RGFiYmxlcg==?=
    Mar 20, 2006
  5. ChemFinder
    Replies:
    0
    Views:
    887
    ChemFinder
    Apr 1, 2006
Loading...

Share This Page