CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

MS SQL 2008 on ASP.NET Hosting



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 6/4/2004 7:16:02 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 5 Views: 47 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
6 Items, 1 Pages 1 |< << Go >> >|
Attila.Net
Asp.Net User
Capturing Events From Dynamically Created Button control6/4/2004 7:16:02 PM

0/0

Hello,

I was wondering if there is a way to capture the events of a dynamic control. For example, I'm dynamically creating Button controls via a PlaceHolder control. The naming convention for the buttons would be button1, button2 ... buttonN. Is there a way to write code that will execute when one of these buttons are clicked, regardless of what which button it was? I don?t know if this is possible.

You help would be much appreciated.

Thanks,
Attila
master4eva
Asp.Net User
Re: Capturing Events From Dynamically Created Button control6/4/2004 8:38:24 PM

0/0

It is possible. Use the normal code of adding the buttons to the control collection:

for (int i = 0; i < 10; i++) {
Button button = new Button();

// ....
button.Click += new EventHandler(Button_Click);
Controls.Add(button);
}

That will simply tell all buttons to execute a single event handler like so:

private void Button_Click(object sender, EventArgs e) {
Button button = (Button)sender; // this is the button that was clicked
}

And from there on, you can just manipulate the button to execute whatever code you want.

Hope that makes sense,
Justin Lovell
-- Justin Lovell
Attila.Net
Asp.Net User
Re: Capturing Events From Dynamically Created Button control6/7/2004 3:07:08 PM

0/0



Thanks for the reply; what you wrote makes sense. However, I've added your code and the event isn't being raised when the button is clicked. Should the code you referenced work?

Attila
master4eva
Asp.Net User
Re: Capturing Events From Dynamically Created Button control6/9/2004 3:39:36 PM

0/0

There is only two reasons why it might not work:

(a) you are not recreating the buttons on post back; or,
(b) your buttons' IDs are changing every post back.

Does that sound familar?
-- Justin Lovell
hinshelm
Asp.Net User
Re: Capturing Events From Dynamically Created Button control6/18/2004 6:03:04 PM

0/0

I am having the same problem. My controls are definetly being recreated on postback and the button ID's are not changing...

Is there anything else that could be the problem as my event is not getting fired..

Code:

Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.IO
Imports System.Web.Security
Imports ProjectNexus.ApplicationTier.UserManagement


Namespace Controls

<System.Web.UI.ParseChildren(True), _
System.Web.UI.ToolboxData("<{0}:NavigationMain runat=server></{0}:NavigationMain>")> _
Public Class NavigationMain
Inherits System.Web.UI.Control
Implements INamingContainer

Private _user As Objects.User = Nothing

Private _objTable As Web.UI.WebControls.Table

Protected ReadOnly Property SiteUser() As Objects.User
Get
Return _user
End Get
End Property

Protected ReadOnly Property Entity() As ApplicationTier.UserManagement.Enumerations.Entity
Get
If SiteUser Is Nothing Then
Dim e As ApplicationTier.UserManagement.Enumerations.Entity
e = CType(System.Web.HttpContext.Current.Session("Entity"), ApplicationTier.UserManagement.Enumerations.Entity)
If IsNothing(e) Then
Return ApplicationTier.UserManagement.Enumerations.Entity.Student
Else
Return e
End If
Else
Return _user.Type
End If
End Get
End Property

Private Property SelectedRealm() As Enumerations.Realms
Get
Return CType(System.Web.HttpContext.Current.Session("SelectedRealm"), Enumerations.Realms)
End Get
Set(ByVal Value As Enumerations.Realms)
System.Web.HttpContext.Current.Session("SelectedRealm") = Value
End Set
'TODO: Reset session vALUE when left navigation clicked
End Property

Public Sub New()
' Attempt to get the current user
Me._user = AppInstance.ManagerFactory.UserManager.GetLoggedOnUser
' Is the user not availabe - must be anonymous
If Me._user Is Nothing Then
AppInstance.ManagerFactory.UserManager.TrackAnonymousUsers()
End If
End Sub 'New

Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
'Setup Table
_objTable = New Web.UI.WebControls.Table
_objTable.ID = "Table"
Me.Controls.Add(_objTable)
End Sub 'CreateChildControls

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
_objTable.ID = "MainNavTable"
_objTable.CellPadding = 0
_objTable.CellSpacing = 0
BuildRealmRow()
BuildRealmSpacerRow()
_objTable.RenderControl(writer)
End Sub

Private Sub AddSpacerCell(ByVal row As TableRow, ByVal BGColor As System.Drawing.Color)
Dim Cell As TableCell
Dim Image As New HtmlControls.HtmlImage
Image.Src = Settings.Site.ApplicationVRoot & "/Imgz/spacer.gif"
Image.Width = 5
Cell = New TableCell
If Not IsNothing(BGColor) Then
Cell.BackColor = BGColor
End If
Cell.Controls.Add(Image)
row.Cells.Add(Cell)
End Sub

Private Sub BuildRealmRow()
Dim Cell As TableCell
Dim Row As TableRow
Row = New TableRow
'Setup navigation Realms
Dim Realms() As String = Settings.Navigation.Realms(Entity)
System.Web.HttpContext.Current.Trace.Write("NavigationMain", Tools.Strings.ArrayToString(Realms))
For i As Integer = 0 To Realms.Length - 1
Dim objRealm As New FormElements.RolloverTab
objRealm.ID = Realms(i)
objRealm.Key = Realms(i) & "Tab"
objRealm.ResourseType = Helpers.Resourses.ResourseType.EnitySpecific
objRealm.MouseDown = True
objRealm.RollOver = True
objRealm.CommandName = Realms(i)
'objRealm.EnableViewState = False
AddHandler objRealm.Click, AddressOf Realm_Click
System.Web.HttpContext.Current.Trace.Write("NaviagationMain:ButtonCreated", Realms(i))
Cell = New TableCell
Cell.Controls.Add(objRealm)
Row.Cells.Add(Cell)
'Add spacer if nessesery
If Not i = Realms.Length - 1 Then
AddSpacerCell(Row, Nothing)
End If
Next
_objTable.Rows.Add(Row)
End Sub

Private Sub BuildRealmSpacerRow()
Dim Cell As TableCell
Dim Row As TableRow
Row = New TableRow
'Setup row spacer for Realms
Dim Realms() As String = Settings.Navigation.Realms(Entity)
For i As Integer = 0 To Realms.Length - 1
If SelectedRealm.ToString = Realms(i) Then
Dim Realm_Color() As String = Settings.Navigation.Realm_Colors(Entity)
AddSpacerCell(Row, System.Drawing.ColorTranslator.FromHtml(Realm_Color(i)))
Else
AddSpacerCell(Row, Nothing)
End If

If Not i = Realms.Length - 1 Then
AddSpacerCell(Row, Nothing)
End If
Next
_objTable.Rows.Add(Row)
End Sub


Private Sub Realm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim But As FormElements.RolloverTab = CType(sender, FormElements.RolloverTab)
System.Web.HttpContext.Current.Trace.Write("NavigationMain:CommandName", But.CommandName)
SelectedRealm = CType([Enum].Parse(GetType(Enumerations.Realms), But.CommandName), Enumerations.Realms)
End Sub


Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
If Not IsNothing(SelectedRealm) Then
Dim objRealm As FormElements.RolloverTab = CType(Me.FindControl(SelectedRealm.ToString), FormElements.RolloverTab)
If Not objRealm Is Nothing Then
objRealm.Selected = True
End If
End If
End Sub

End Class

End Namespace

Martin Hinshelwood's Blog
master4eva
Asp.Net User
Re: Capturing Events From Dynamically Created Button control6/18/2004 7:39:49 PM

0/0

I have already answered your thread. For anybodies interests' sake, the thread can be found here:

view post 609123
-- Justin Lovell
6 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Microsoft Visual C# .NET 2003 Developer's Cookbook: Developer's Cookbook Authors: Mark Schmidt, Simon Robinson, Pages: 787, Published: 2003
The Book of Visual Basic 2005: .NET Insight for Classic VB Developers Authors: Matthew MacDonald, Pages: 490, Published: 2006
Mastering JavaServer Faces Authors: Bill Dudney, Jonathan Lehr, Bill Willis, LeRoy Mattingly, Pages: 456, Published: 2004
Programming Ruby: The Pragmatic Programmer's Guide Authors: David Thomas, Andrew Hunt, Pages: 564, Published: 2000
A Practical Guide to Curl Authors: Kevin Hanegan, Pages: 398, Published: 2003
JavaScript: The Complete Reference Authors: Thomas A. Powell, Fritz Schneider, Pages: 948, Published: 2004
Platinum Edition Using XHTML, XML and Java 2: Using XHTML, XML and Java 2 Authors: Eric Ladd, Jim O'Donnell, Mike Morgan, Andrew H. Watt, Pages: 1413, Published: 2001
Microsoft Visual C# .Net Step by Step: step by step Authors: John Sharp, Jon Jagger, Pages: 621, Published: 2002

Web:
UserControl Events Not Firing with Dynamically Created Button ... UserControl Events Not Firing with Dynamically Created Button Control. ... click the button it seems to fire the events but I'm unable to capture ...
Solution to Dynamically-created-controls Event firing problem ... I add a button control to one of the cells during the build, expecting to be able to capture the 'click' event of the button and thru code ...
C# ASP.net events on dynamically created controls - Topic Powered ... If you dynamically create a control - regardless of whether you bind an .... my point is capturing the events from dynamically created items ...
ASP.NET Web Server Control Event Model For example, each row in a GridView control can contain one or more buttons created dynamically by templates. Rather than each button raising an event ...
Thom Robbins .NET Weblog : Dynamic controls and event handlers For example, we will create an ASP.NET form that contains a dynamic button and label control. Once the button is pressed we want to capture the event and ...
senocular.com Tutorial: Dealing With Flash Button Event Capturing ... Flash uses event capturing when handling button events coming from the user. ..... A single function can be created to assign these methods dynamically with ...
Capturing the click event of button inside a dynamically loaded ... Capturing the click event of button inside a dynamically loaded user control in asp.net VB ... The control has to be created in your button click event, ...
help me to create a dynamic button control.... - Windows Forms - I'm trying to capture the editing event and, if an exclusive selection is made, .... i'm trying to create a button control dynamically in ASP.Net using C#. ...
Dynamic ASP.NET button control - .NET ASP Dynamic ASP.NET button control. Get answers to your questions in our . ... capture the event and execute some code in the codebehind file? ...
Ajax control toolkit Modalpopup extender events when i select this button the modal form closes. I need to capture the click event on ... The modal popup exists within a dynamically created user control, ...

Videos:
XML11: An Abstract Windowing Protocol Google TechTalks June 1, 2006 Arno Puder Arno Puder received his masters and Ph.D. in computer science and is currently working as an Assistant Prof...




Search This Site:










how is this for colocation costs...

problem with custom dropdownlist control

simple user control question

validator inside of composite control pointing to an outside control

adding behaviour to a adrotator control

copying a web porject

got hacked ???

id converted to unique id

cookie expiry

help creating static image in my server control

determine clientids within a usercontrol's code ?

customize smart tag..

best practice for configuring custom address listed for hosted exchange?

simple button control

design time to datagrid(inherited)

which is the best database to use for a site using shared-hosting?

nested user controls

new control id separator ($) breaks legacy codes

checkbox/radiobutton - some attributes going to span

where to put validators?

calling javascript function oncollapse of treeview control

user control : nested content

problems with viewstate

system explore control

raisepostbackeventhandler running twice?

default values for custom control property

custom control within a control control

why won't this work?!

need an "editablelabel"

custom collection in a composite control

  Privacy | Contact Us
All Times Are GMT