Results 1 to 4 of 4

Thread: Creating dynamic controls with events

  1. #1
    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.

  2. #2
    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

  3. #3
    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.

  4. #4
    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

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,424,115.57076 seconds with 16 queries