Jan 05, 2012, 3:56 PM
Using AddHandler in dynamic controls
Hi there.
I am having troubles with using AddHandler in dynamic controls.
I used this example [http://forums.ext.net/showthread.php...d-dynamically]
But form some reason the function [window_CloseEvent] never raises. That function was added to the control event using AddHandler.
I recreate the dynamic controls in Page_Init, I recreate the AddHandler too, I am sure than the controls doesnt lose on the postbacks. I seaid this because until now, I can add, delete, dynamic controls and I do not lose them in the postbacks.
I think I use all the stuff showed in the ext.net example, but there is something I am missing and I need to know what is it.
I am in this trouble by several hours.
My page has plenty of code, and I add here the part of code for this trouble.
Can you help me with this?
Thanks in advanced.
The part of code than create the dynamic control with text controls inside:
I am having troubles with using AddHandler in dynamic controls.
I used this example [http://forums.ext.net/showthread.php...d-dynamically]
But form some reason the function [window_CloseEvent] never raises. That function was added to the control event using AddHandler.
I recreate the dynamic controls in Page_Init, I recreate the AddHandler too, I am sure than the controls doesnt lose on the postbacks. I seaid this because until now, I can add, delete, dynamic controls and I do not lose them in the postbacks.
I think I use all the stuff showed in the ext.net example, but there is something I am missing and I need to know what is it.
I am in this trouble by several hours.
My page has plenty of code, and I add here the part of code for this trouble.
Can you help me with this?
Thanks in advanced.
The part of code than create the dynamic control with text controls inside:
Imports Microsoft.VisualBasic
Imports Ext.Net
Namespace Gravitar.Neptune.Util.Dynamic
Public Class SolicitudeStopContact
Inherits FormPanel
Public iIdSolicitudeStopContact As TextField
Public sContactName As TextField
Public sEmail As TextField
Public sPhone As TextField
Public sMobile As TextField
Dim iLanguaje As Integer
Public Delegate Sub DestroyOnClient(ByVal ctrl As XControl)
Public Event CloseEvent As DestroyOnClient
Public Sub New(ByVal Languaje As ConstantsApp.eLanguaje)
MyBase.New()
iLanguaje = Languaje
End Sub
Protected Overrides Sub OnInit(e As System.EventArgs)
MyBase.OnInit(e)
Me.SuspendScripting()
Dim oLanguage_SolicitudeStopContact As New Gravitar.Neptune.Lang.Solicitude.SolicitudeStopContact(Me.iLanguaje)
Me.Layout = "AnchorLayout" 'anchor
'Panel principal
Dim pnlPrincipal As Panel = New Panel()
pnlPrincipal.Border = False
pnlPrincipal.Height = "55"
pnlPrincipal.Padding = "2"
Me.Items.Add(pnlPrincipal)
'Panel1
Dim pnlPanel1 As Panel = New Panel()
pnlPanel1.Border = False
pnlPanel1.Height = "25"
pnlPanel1.Layout = "HBoxLayout"
Dim HBoxLayoutConfig1 As HBoxLayoutConfig = New HBoxLayoutConfig()
HBoxLayoutConfig1.Align = HBoxAlign.Middle
pnlPanel1.LayoutConfig.Add(HBoxLayoutConfig1)
pnlPrincipal.Add(pnlPanel1)
'Botón de cierre
Dim cmdClose As Ext.Net.Button = New Ext.Net.Button()
cmdClose.ID = String.Format("{0}_{1}_{2}", Me.ID, "Button", "CloseButton")
cmdClose.Icon = Ext.Net.Icon.PageWhiteDelete
'b.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(CloseClick); 'C# version
AddHandler cmdClose.DirectEvents.Click.Event, AddressOf Me.CloseClick
pnlPanel1.Items.Add(cmdClose)
Dim FormPanel1 As FormPanel = New FormPanel()
FormPanel1.Border = False
FormPanel1.Layout = "AnchorLayout"
FormPanel1.Flex = "1"
FormPanel1.Padding = "2"
pnlPanel1.Items.Add(FormPanel1)
Dim FormPanel2 As FormPanel = New FormPanel()
FormPanel2.Border = False
FormPanel2.Layout = "AnchorLayout"
FormPanel2.Flex = "1"
FormPanel2.Padding = "2"
pnlPanel1.Items.Add(FormPanel2)
'sContactName
Me.sContactName = New TextField
Me.sContactName.ID = String.Format("{0}_{1}_{2}", Me.ID, "TextField", "sContactName")
Me.sContactName.AnchorHorizontal = "100%"
Me.sContactName.FieldLabel = oLanguage_SolicitudeStopContact.langFieldName_sContactName '"sContactName"
FormPanel1.Items.Add(sContactName)
'sEmail
Me.sEmail = New TextField
Me.sEmail.ID = String.Format("{0}_{1}_{2}", Me.ID, "TextField", "sEmail")
Me.sEmail.AnchorHorizontal = "100%"
Me.sEmail.FieldLabel = oLanguage_SolicitudeStopContact.langFieldName_sEmail '"sEmail"
FormPanel2.Items.Add(sEmail)
'Panel2
Dim pnlPanel2 As Panel = New Panel()
pnlPanel2.Border = False
pnlPanel2.Height = "25"
pnlPanel1.Layout = "HBoxLayout"
Dim HBoxLayoutConfig2 As HBoxLayoutConfig = New HBoxLayoutConfig()
HBoxLayoutConfig2.Align = HBoxAlign.Middle
pnlPanel2.LayoutConfig.Add(HBoxLayoutConfig2)
pnlPrincipal.Add(pnlPanel2)
'Dummy space
Dim FormPanel5 As FormPanel = New FormPanel()
FormPanel5.Border = False
FormPanel5.Width = "20"
pnlPanel2.Items.Add(FormPanel5)
Dim FormPanel3 As FormPanel = New FormPanel()
FormPanel3.Border = False
FormPanel3.Layout = "AnchorLayout"
FormPanel3.Flex = "1"
FormPanel3.Padding = "2"
pnlPanel2.Items.Add(FormPanel3)
Dim FormPanel4 As FormPanel = New FormPanel()
FormPanel4.Border = False
FormPanel4.Layout = "AnchorLayout"
FormPanel4.Flex = "1"
FormPanel4.Padding = "2"
pnlPanel2.Items.Add(FormPanel4)
'sPhone
Me.sPhone = New TextField
Me.sPhone.ID = String.Format("{0}_{1}_{2}", Me.ID, "TextField", "sPhone")
Me.sPhone.AnchorHorizontal = "100%"
Me.sPhone.FieldLabel = oLanguage_SolicitudeStopContact.langFieldName_sPhone '"sPhone"
FormPanel3.Items.Add(sPhone)
'sMobile
Me.sMobile = New TextField
Me.sMobile.ID = String.Format("{0}_{1}_{2}", Me.ID, "TextField", "sMobile")
Me.sMobile.AnchorHorizontal = "100%"
Me.sMobile.FieldLabel = oLanguage_SolicitudeStopContact.langFieldName_sMobile '"sMobile"
FormPanel4.Items.Add(sMobile)
'iIdSolicitudeStopContact
Me.iIdSolicitudeStopContact = New TextField
Me.iIdSolicitudeStopContact.ID = String.Format("{0}_{1}_{2}", Me.ID, "TextField", "iIdSolicitudeStopContact")
Me.iIdSolicitudeStopContact.Hidden = True
Me.iIdSolicitudeStopContact.FieldLabel = oLanguage_SolicitudeStopContact.langFieldName_iIdSolicitudeStopContact '"iIdSolicitudeStopContact"
Me.Items.Add(iIdSolicitudeStopContact)
Me.ResumeScripting()
End Sub
Private Sub CloseClick(ByVal sender As Object, ByVal e As EventArgs)
Me.Destroy() 'Destruir (y por ende ocultar) el control del lado del cliente
RaiseEvent CloseEvent(Me) 'invocar al evento que llama a la función [RemoveFromRecreation] que borra el nombre de la lista genérica
End Sub
End Class
End Namespace
The Page_Init code Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
Me.Session.Remove("CreatedWindows")
End If
Dim ids As System.Collections.Generic.List(Of String) = Me.Session("CreatedWindows")
If Not ids Is Nothing Then
For Each id As String In ids
Dim w As Dynamic.SolicitudeStopContact = New Dynamic.SolicitudeStopContact(Me.oSesion.iLenguaje)
w.ID = id
AddHandler w.CloseEvent, AddressOf Me.window_CloseEvent 'This line was added by me (It was not in the Ext.Net forum example).
Me.Container_StopContact.Add(w)
Next
End If
End Sub
The part of code in VB than uses the dynamic controls: #Region "Métodos de Uso de Controles Dinámicos"
Protected Sub btnNew_SolicitudeStopContact_DirectClick(sender As Object, e As Ext.Net.DirectEventArgs) Handles btnNew_SolicitudeStopContact.DirectClick
Dim id As String = String.Format("{0}{1}", "MyObjectNew", Shared_iDynamicControlCounter)
Shared_iDynamicControlCounter += 1
Me.AddToRecreation(id)
Dim w As Dynamic.SolicitudeStopContact = New Dynamic.SolicitudeStopContact(Me.oSesion.iLenguaje)
w.ID = id
AddHandler w.CloseEvent, AddressOf Me.window_CloseEvent
Me.Container_StopContact.Add(w)
w.Render()
End Sub
Private Sub AddToRecreation(ByVal sControlId As String)
Dim ids As System.Collections.Generic.List(Of String) = Me.Session("CreatedWindows")
If ids Is Nothing Then
ids = New System.Collections.Generic.List(Of String)
Me.Session("CreatedWindows") = ids
End If
ids.Add(sControlId)
End Sub
Private Sub RemoveAllFromRecreation()
'Kill all dynamic controls
Dim ids As System.Collections.Generic.List(Of String) = Me.Session("CreatedWindows")
If Not ids Is Nothing Then
For Each id As String In ids
Dim w As Dynamic.SolicitudeStopContact = Container_StopContact.FindControl(id)
w.Destroy() 'Destruir el control del lado del cliente
Next
ids.Clear()
End If
End Sub
Private Sub RemoveFromRecreation(ByVal sControlId As String)
Dim ids As System.Collections.Generic.List(Of String) = Me.Session("CreatedWindows")
'TODO: mandar llamar a la función que eliminará el contacto
'pero primero asegurarse si el ID es del tipo New o no
'---
If Not ids Is Nothing Then
ids.Remove(sControlId)
End If
End Sub
Private Sub LoadRecordToDB_SolicitudeStopContact(ByVal iIdSolicitudeStop As Integer)
'Primero ciclar la "recreation"
Dim ids As System.Collections.Generic.List(Of String) = Me.Session("CreatedWindows")
If Not ids Is Nothing Then
For Each id As String In ids
Dim w As Dynamic.SolicitudeStopContact = Container_StopContact.FindControl(id)
w.ID = id
'Luego llamar al ABC
Dim oDataObject As New SolicitudeStopContact
oDataObject.iIdSolicitude = Me.txtiIdSolicitude.Text
oDataObject.iIdSolicitudeStop = iIdSolicitudeStop
oDataObject.iIdSolicitudeStopContact = IIf(w.ID.Contains("MyObjectNew"), "0", w.iIdSolicitudeStopContact)
oDataObject.sContactName = w.sContactName.Text
oDataObject.sEmail = w.sEmail.Text
oDataObject.sPhone = w.sPhone.Text
oDataObject.sMobile = w.sMobile.Text
oDataObject.iIdLoggedUser = oSesion.iIdEmpleado
If w.ID.Contains("MyObjectNew") = True Then
If Not oDataObject.Create() Then
ExtMessageBox.ShowAlert(Me.langMessageTitle_Error, Me.langMessageBody_CreateError, Me.langMessageButton_Ok)
Exit Sub
Else
'ExtMessageBox.ShowAlert(Me.langMessageTitle_Notification, Me.langMessageBody_CreateSuccess, Me.langMessageButton_Ok)
End If
Else
If Not oDataObject.Update Then
ExtMessageBox.ShowAlert(Me.langMessageTitle_Error, Me.langMessageBody_UpdateError, Me.langMessageButton_Ok)
Exit Sub
Else
'ExtMessageBox.ShowAlert(Me.langMessageTitle_Notification, Me.langMessageBody_UpdateSuccess, Me.langMessageButton_Ok)
End If
End If
w.Destroy()
Next
End If
End Sub
Private Sub LoadRecordToForm_SolicitudeStopContact(ByVal iIdSolicitudeStop As Integer)
'Primero traer en una tabla todos los contactos
Dim oDataObject As New SolicitudeStopContact
Dim oDt As DataTable = Nothing
oDataObject.iIdSolicitude = Me.txtiIdSolicitude.Text
oDataObject.iIdSolicitudeStop = iIdSolicitudeStop
oDt = oDataObject.List()
If Not oDt Is Nothing AndAlso oDt.Rows.Count > 0 Then
'Luego ciclar la tabla
For Each dr As DataRow In oDt.Rows
Dim id As String = String.Format("{0}{1}", "MyObjectOld", Shared_iDynamicControlCounter)
Shared_iDynamicControlCounter += 1
Me.AddToRecreation(id)
Dim w As Dynamic.SolicitudeStopContact = New Dynamic.SolicitudeStopContact(Me.oSesion.iLenguaje)
w.ID = id
AddHandler w.CloseEvent, AddressOf Me.window_CloseEvent
Me.Container_StopContact.Add(w)
w.iIdSolicitudeStopContact.Text = dr("iIdSolicitudeStopContact").ToString
w.sContactName.Text = dr("sContactName").ToString()
w.sEmail.Text = dr("sEmail").ToString()
w.sPhone.Text = dr("sPhone").ToString()
w.sMobile.Text = dr("sMobile").ToString()
w.Render()
Next
End If
End Sub
Sub window_CloseEvent(ByVal ctrl As XControl)
Me.RemoveFromRecreation(ctrl.ID)
End Sub
#End Region
The html code:...
<ext:Container ID="Container_StopContact" runat="server" AutoHeight="true" />
...
My page behavior is the same if I delete this line:AddHandler w.CloseEvent, AddressOf Me.window_CloseEvent
Last edited by kabalkunz; Jan 08, 2012 at 8:31 PM.