newby question

J

jetroy

I have a question about a little .aspx program
----------------------------------------------------
<%@ Page Language="c#" %>
<script runat="server">
void ChangeLabel(Object Sender, EventArgs E)
{
label1.Text = "<BR> You clicked: " + Sender.ToString();
}
</script>
<html>
<body>
<h2>Using the Button control </h2>
<form runat="server">
A Label Control: <asp:Label id="label1" runat="server"></asp:Label>
<p>
<asp:Button id="Button1" onclick="ChangeLabel" runat="server" text="Change
Label"></asp:Button>
<asp:Button id="Button2" onclick="ChangeLabel" runat="server" text="Change
Label"></asp:Button>
</form>
</body>
</html>
 
M

Morten Wennevik

Hi jetroy,

ASP.NET isn't what I know most of, but I would imagine this would do it:

label1.Text = "<BR> You clicked: " + Sender.ID;
 
G

Guest

Hi jetroy,

You can use the CommandEventArgs to determine which button was clicked. All
you have to do is change the onclick that you have in your button to
OnCommand and add a CommandName to the button. Then change the EventArgs in
your function to CommandEventArgs. Now there will be two more exposed
properties CommandArgument and CommandName. You can simply look at the
CommandName to determine which button was clicked. I have changed your
sample to demonstrate. Please see below.

Hope this helps.
---------------
<%@ Page language="c#" %>
<HTML>
<script runat="server">
public void ChangeLabel(Object Sender, CommandEventArgs e)
{
label1.Text = e.CommandName;
}
</script>
<body>
<h2>Using the Button control
</h2>
<form runat="server" ID="Form1">
A Label Control:
<asp:Label id="label1" runat="server"></asp:Label>
<p>
<asp:Button id="Button1" CommandName="Button1" OnCommand="ChangeLabel"
runat="server" text="Change Label"></asp:Button>
<asp:Button id="Button2" CommandName="Button2" OnCommand="ChangeLabel"
runat="server" text="Change Label"></asp:Button>
</form>
</P>
</body>
</HTML>
 

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

Top