Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-05-2009
Member
 
Join Date: Apr 2009
Posts: 90
Creating dynamic controls with events

Hello everyone,

I wonder if it is possible to assign an event management control to a dynamically created AFTER the Page_Load?

The control is created as expected but the events are not taken into account.


Thank you in advance.
Reply With Quote
  #2  
Old 11-05-2009
Member
 
Join Date: Oct 2008
Posts: 167
Re: Creating dynamic controls with events

Easy way to check whether created dynamic controls are working appropriately you need to create window application that need to handle events their Attributes need not to be come in between while making any changes and adding controls dynamically, which will handle events, to that window.

Code:
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
  Dim btn As Button  
  
 For i As Integer = 0 To 10  
      btn = New Button  
      btn.Text = "Button" & i  
      btn.ID = "Button" & i  
     AddHandler btn.Click, AddressOf btn_Click  
     MyPanel.Controls.Add(btn)  
       Next  
 
 End Sub  
  
 Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)  
     Response.Write("You clicked " & CType(sender, Button).ID)  
 End Sub
Reply With Quote
  #3  
Old 11-05-2009
Member
 
Join Date: Jan 2006
Posts: 211
Re: Creating dynamic controls with events

You can get GetEvent method on the Type to get the EventInfo that
represents the event. Start creating controls in the Page.PreInit event. Move your existing code from the Load event to PreInit, and look to see if the page is posting back. If not, do what you are currently doing. If it is posting back, save the data about your controls (i.e. how many and names) in Session variables.Once you have that, you can call the AddEventHandler method on the EventInfo instance to add the event to the control.
Reply With Quote
  #4  
Old 11-05-2009
Member
 
Join Date: Dec 2008
Posts: 183
Re: Creating dynamic controls with events

please check the code and try for your reference.

Code:
Dim t As Textbox = pnlPanel.FindControl("txtText")

If t IsNot Nothing Then
    t.Text = "Found Control!
Else
   Error message
End If
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Creating dynamic controls with events"
Thread Thread Starter Forum Replies Last Post
What are dynamic controls in DHTML Maximiliano2 Software Development 5 13-11-2010 06:30 AM
All day events one day out Steadfast Portable Devices 4 13-10-2010 11:22 AM
Creating Controls In A Loop technika Software Development 5 05-02-2010 10:34 PM
Creating dynamic pages with asp to redirect Isdel Software Development 3 20-11-2009 10:39 PM
What are those controls that do not have events? Chandrakant81 Software Development 3 27-02-2009 10:40 PM


All times are GMT +5.5. The time now is 10:50 AM.