[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Cath

1/14/2003 4:47:00 AM

I dynamically created several buttons.... How do I set a common handler for
all of them, passing 1 argument? What's a sample code for that?

Thanks.


2 Answers

(Patrick C. Cole (MS))

1/14/2003 6:31:00 PM

0

Cath,

Before you add the button (using Controls.Add), set the event handler to
the specific event you need. For example:

C#
btnMyButton.Text = "My Button";
btnMyButton.ID = "btnMyButton";
//
// other properties for the button
//
btnMyButton.Click += new System.EventHandler(this.myButtonHandler_Click);
// add button to location using "Controls.Add"

VB
btnMyButton.Text = "My Button"
btnMyButton.ID = "btnMyButton"
'
' other properties for the button
'
AddHandler btnMyButton.Click, AddressOf Me.myButtonHandler_Click)
' add button to location using "Controls.Add"


The event handler for myButtonHandler would look like the following:

C#
private void myButtonHandler_Click(object sender, System.EventArgs e)
{
// btnTemp will be the button clicked by the user
Button btnTemp = ((Button)sender);
// perform any actions you need based on the button click
}

VB
Private Sub myButtonHandler_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
' btnTemp will be the button clicked by the user.
Dim btnTemp As Button = CType(sender, Button)
' perform any actions you need based on the button click
End Sub

It is important that you give an ID to the dynamic controls. You will not
be able to reliably determine what control performed the postback unless
you set the ID property.

Hope this helps,

Patrick Cole
Microsoft Developer Communities

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Cath" <cvictor@mydestiny.net>
| Subject: button handlers
| Date: Tue, 14 Jan 2003 11:47:31 +0800
| Lines: 6
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
| Message-ID: <eCvQE$3uCHA.616@TK2MSFTNGP11>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 202.8.242.43
| Path:
cpmsftngxa06!cpmsftngxa08!cppssbbsa01.microsoft.com!TK2MSFTNGP08!TK2MSFTNGP1
1
| Xref: cpmsftngxa06
microsoft.public.dotnet.framework.aspnet.webcontrols:8497
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I dynamically created several buttons.... How do I set a common handler
for
| all of them, passing 1 argument? What's a sample code for that?
|
| Thanks.
|
|
|

(Patrick C. Cole (MS))

1/14/2003 6:35:00 PM

0

Cath,

As for the argument, you can use the buttons CommandArgument collection to
pass information back to the handler.

Let me know if you need more help with this.

Patrick Cole
Microsoft Developer Communities

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved.
--------------------
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| From: patcole@online.microsoft.com (Patrick C. Cole [MSFT])
| Organization: Microsoft
| Date: Tue, 14 Jan 2003 17:31:57 GMT
| Subject: RE: button handlers
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
|
| Cath,
|
| Before you add the button (using Controls.Add), set the event handler to
| the specific event you need. For example:
|
| C#
| btnMyButton.Text = "My Button";
| btnMyButton.ID = "btnMyButton";
| //
| // other properties for the button
| //
| btnMyButton.Click += new System.EventHandler(this.myButtonHandler_Click);
| // add button to location using "Controls.Add"
|
| VB
| btnMyButton.Text = "My Button"
| btnMyButton.ID = "btnMyButton"
| '
| ' other properties for the button
| '
| AddHandler btnMyButton.Click, AddressOf Me.myButtonHandler_Click)
| ' add button to location using "Controls.Add"
|
|
| The event handler for myButtonHandler would look like the following:
|
| C#
| private void myButtonHandler_Click(object sender, System.EventArgs e)
| {
| // btnTemp will be the button clicked by the user
| Button btnTemp = ((Button)sender);
| // perform any actions you need based on the button click
| }
|
| VB
| Private Sub myButtonHandler_Click(ByVal sender As System.Object, ByVal e
As
| System.EventArgs)
| ' btnTemp will be the button clicked by the user.
| Dim btnTemp As Button = CType(sender, Button)
| ' perform any actions you need based on the button click
| End Sub
|
| It is important that you give an ID to the dynamic controls. You will
not
| be able to reliably determine what control performed the postback unless
| you set the ID property.
|
| Hope this helps,
|
| Patrick Cole
| Microsoft Developer Communities
|
| This posting is provided "AS IS" with no warranties, and confers no
rights.
| You assume all risk for your use. © 2002 Microsoft Corporation. All
rights
| reserved.
| --------------------
| | From: "Cath" <cvictor@mydestiny.net>
| | Subject: button handlers
| | Date: Tue, 14 Jan 2003 11:47:31 +0800
| | Lines: 6
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
| | Message-ID: <eCvQE$3uCHA.616@TK2MSFTNGP11>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| | NNTP-Posting-Host: 202.8.242.43
| | Path:
|
cpmsftngxa06!cpmsftngxa08!cppssbbsa01.microsoft.com!TK2MSFTNGP08!TK2MSFTNGP1
| 1
| | Xref: cpmsftngxa06
| microsoft.public.dotnet.framework.aspnet.webcontrols:8497
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| |
| | I dynamically created several buttons.... How do I set a common handler
| for
| | all of them, passing 1 argument? What's a sample code for that?
| |
| | Thanks.
| |
| |
| |
|