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

M

Mike

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
 
A

Alessandro Zifiglio

hi mike,
protected void Mday_Click(object sender, System.EventArgs e)
{
this.Response.Write("You clicked the " + e.CommandName + " button.");
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top