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.
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.
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
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.
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
Bookmarks