473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

newby question



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>
--------------------------------------------------
The program works OK, but how could it be changed so as to
identify which button caused the label1.text message. In other words I
would like to change "sender.ToString()" into something that would display
"Button1" or "Button2" depending on which button was clicked.
Nov 16 '05 #1
4 1095
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;
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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>
Nov 16 '05 #3
Thanks, but Sender.id is the first thing that I tried but it won't work.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Thanks very much, I tried it out and it works.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
0
by: Pete | last post by:
Hi All, A total Newby with, possibly, a daft question? However, until I can get a reasonable explanation I am disinclined towards going further. Here goes: I recently downloaded the latest...
8
by: Ask | last post by:
G'day All, Just thought I'd drop in and say hi. I'm new to Python, but old to software development. Python is one of the languages used in my new job, so I've just bought a book, read it, and...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
4
by: Fred Nelson | last post by:
I have an applicatioin that I'm writing that uses a "case" file that contains over 350 columns and more may be added in the future. I would like to create a dataset with all the column names and...
4
by: Fred Nelson | last post by:
Hi: I'm developing a web application that needs to have five values, each retrieved from cookies on many pages. If I have five "Request.Cookies" commands together does this cause five "round...
2
by: Fred Nelson | last post by:
Hi: I'm working on a VS2005 web application and I have what is probabably a "newby" question. In VS2003 I could drag a textbox/button/etc on to a form and position it with the mouse. I...
2
by: johnnyG | last post by:
Greetings, I'm studying for the 70-330 Exam using the MS Press book by Tony Northrup and there are 2 side-by-side examples of using the SHA1CryptoServiceProvider to create a hash value from a...
10
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
5
by: alexrixhardson | last post by:
Hi guys, I am a newby in the C/C++ world, and I am beginning to work on a rather simple TCP/IP proxy application which must be able to handle large volume of data as quickly as possible. ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.