Hi,
From the dynamic form added. After having filled my form, When you click click on the button "btnSubmit" it returns to me that there is no controller in the panel "pnlForm"; the "pnlForm.Controls.Count" line 37 returns 0.
it's as if the Page.PostBack was done before the btnSubmit.Click and the "Viewstate" is only displayed in the "litRsltDebug" after a second click
Thank's for your help :)
<section>
<article class="container full">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 col-h ">
<asp:panel id="pnlForm" cssclass="col-lng" runat="server" />
<asp:Button ID="btnSubmit" Text="submit" runat="server" />
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 col-h text-center">
<asp:literal ID="litRslt" Text="RSLT" runat="server" />
<asp:literal ID="litRsltDebug" Text="N/A" runat="server" />
</div>
</div>
</article>
</section>
Imports Newtonsoft.Json
Partial Class Page_DynamicForm
Inherits System.Web.UI.Page
Private BagDatasFormRlst As String = ""
Private BagDatasFormDebug As String = ""
Private Sub Page_DynamicForm_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LoadFormConstruct()
ViewState("BagDatasFormRlst") = BagDatasFormRlst
ViewState("BagDatasFormDebug") = BagDatasFormDebug
Else
BagDatasFormRlst = ViewState("BagDatasFormRlst")
BagDatasFormDebug = ViewState("BagDatasFormDebug")
End If
litRsltDebug.Text = BagDatasFormDebug & "<br/>" & BagDatasFormRlst
If BagDatasFormRlst <> "" Then ViewRslt(BagDatasFormRlst)
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim debug As String = Now.ToString & "<br/>"
Dim DT_Rslt As New DataTable
Try
DT_Rslt = GetTblRslt()
Catch ex As Exception
ConsoleLogJS(Me.Page, "(btnSubmit_Click) " & "LOAD DT_RSLT : " & ex.ToString, "x")
debug &= "(btnSubmit_Click) " & "LOAD DT_RSLT : " & ex.ToString & "<br/>"
End Try
Try
If (DT_Rslt IsNot Nothing) Then
Dim datas As New Dictionary(Of String, String)
ConsoleLogJS(Me.Page, "ctrl.count : " & pnlForm.Controls.Count, "x")
For Each ctrl As Control In pnlForm.Controls
debug &= "ctrl : " & ctrl.ID
ConsoleLogJS(Me.Page, "ctrl : " & ctrl.ID, "x")
If TypeOf ctrl Is System.Web.UI.WebControls.RadioButton Then
Dim rbt As System.Web.UI.WebControls.RadioButton = DirectCast(ctrl, System.Web.UI.WebControls.RadioButton)
datas.Add(rbt.ID, rbt.Checked)
End If
If TypeOf ctrl Is System.Web.UI.WebControls.CheckBox Then
Dim chk As System.Web.UI.WebControls.CheckBox = DirectCast(ctrl, System.Web.UI.WebControls.CheckBox)
datas.Add(chk.ID, chk.Checked)
End If
If TypeOf ctrl Is System.Web.UI.WebControls.TextBox Then
Dim tbx As System.Web.UI.WebControls.TextBox = DirectCast(ctrl, System.Web.UI.WebControls.TextBox)
datas.Add(tbx.ID, tbx.Text)
End If
Next
' ///////////////////////////
If datas.Count > 0 Then
Dim i As Integer = 0
For Each kvp As KeyValuePair(Of String, String) In datas
Dim k As String = kvp.Key
Dim v As String = kvp.Value
DT_Rslt.Rows(0)("idInput" & i.ToString & "DT") = kvp.Key
DT_Rslt.Rows(0)("valueInput" & i.ToString & "DT") = kvp.Value
i += 0
Next
Else
ConsoleLogJS(Me.Page, "(btnSubmit_Click) " & "datas : NOT DATAS !!!", "x")
debug &= "(btnSubmit_Click) " & "datas : NOT DATAS !!!" & "<br/>"
End If
BagDatasFormRlst = JsonConvert.SerializeObject(DT_Rslt)
Else
ConsoleLogJS(Me.Page, "(btnSubmit_Click) " & "DT_RSLT : NOT EXIST !!!", "x")
debug &= "(btnSubmit_Click) " & "DT_RSLT : NOT EXIST !!!" & "<br/>"
End If
Catch ex As Exception
ConsoleLogJS(Me.Page, "(btnSubmit_Click) " & "Try Form : " & ex.ToString, "x")
debug &= "(btnSubmit_Click) " & "Try Form : " & ex.ToString & "<br/>"
End Try
BagDatasFormDebug = debug
ViewState("BagDatasFormRlst") = BagDatasFormRlst
ViewState("BagDatasFormDebug") = BagDatasFormDebug
End Sub
#Region "ViewRslt"
Private Sub ViewRslt(datas As String)
If datas <> "" Then
ConsoleLogJS(Me.Page, "(ViewRslt) " & datas, "x")
Dim output As String = ""
Try
Dim DT_Rslt As New DataTable
Try
DT_Rslt = GetTblRslt()
DT_Rslt = JsonConvert.DeserializeObject(Of DataTable)(datas)
Catch ex As Exception
ConsoleLogJS(Me.Page, "(btnSubmit_Click) " & "LOAD DT_RSLT : " & ex.ToString, "x")
End Try
If DT_Rslt.Rows.Count > 0 Then
Dim itemId As String = "", itemValue As String = ""
For i = 0 To DT_Rslt.Rows.Count - 1
For j = 0 To nbColTblRslt
itemId = CStr(DT_Rslt.Rows(i)("idInput" & j.ToString & "DT"))
itemValue = CStr(DT_Rslt.Rows(i)("valueInput" & j.ToString & "DT"))
output &= itemId & " : " & itemValue & ";" & "<br/>"
Next
Next
litRslt.Text = output
Else
ConsoleLogJS(Me.Page, "(ViewRslt) " & "NOT RSLT !", "dbg")
End If
Catch ex As Exception
ConsoleLogJS(Me.Page, "(ViewRslt) " & ex.ToString, "x")
End Try
Else
ConsoleLogJS(Me.Page, "(ViewRslt) " & "NOT DATAS !", "dbg")
End If
End Sub
#End Region
#Region "FormConstruct"
Private Sub LoadFormConstruct()
Dim DT As New DataTable
Try
DT = GetListOfInput()
Catch ex As Exception
ConsoleLogJS(Me.Page, "(LoadFormConstruct) " & "LOAD DATAS_INPUT : " & ex.ToString, "x")
End Try
Try
If DT.Rows.Count > 0 Then
Dim id As String = "", title As String = "", describe As String = "", value As String = ""
Dim type As String = "", tooltip As String = "", enabled As Boolean = False, groupname As String = ""
For i As Integer = 0 To DT.Rows.Count - 1
id = CStr(DT.Rows(i)("idDT").ToString())
type = CStr(DT.Rows(i)("typeDT").ToString())
tooltip = CStr(DT.Rows(i)("tooltipDT").ToString())
value = CStr(DT.Rows(i)("valueDT").ToString())
enabled = CBool(DT.Rows(i)("enabledDT"))
groupname = CStr(DT.Rows(i)("groupNameDT").ToString())
title = CStr(DT.Rows(i)("titleDT").ToString())
'describe = CStr(DT.Rows(i)("describeDT").ToString())
If type <> "" Then
Dim lbl As New System.Web.UI.WebControls.Label : lbl.ID = id & "LBL" : lbl.Text = title
Dim dCtrl As Object = Nothing
If type = "tbx" Or type = "txt" Then
Dim tbx As New System.Web.UI.WebControls.TextBox
tbx.ID = id
tbx.Text = value
tbx.ToolTip = tooltip
tbx.Enabled = enabled
If type = "txt" Then tbx.TextMode = TextBoxMode.MultiLine
dCtrl = tbx
End If
If type = "chk" Then
Dim chk As New System.Web.UI.WebControls.CheckBox
chk.ID = id
chk.Checked = If(((LCase(value) = "true") Or (value = "1")), (True), (False))
chk.ToolTip = tooltip
chk.Enabled = enabled
dCtrl = chk
End If
If type = "rbt" Then
Dim rbt As New System.Web.UI.WebControls.RadioButton
rbt.ID = id
rbt.Checked = If(((LCase(value) = "true") Or (value = "1")), (True), (False))
rbt.ToolTip = tooltip
rbt.Enabled = enabled
If groupname <> "" Then rbt.GroupName = groupname
dCtrl = rbt
End If
If dCtrl IsNot Nothing Then
Dim pnl As New System.Web.UI.WebControls.Panel
pnl.ID = id & "PNL"
pnl.CssClass = "col-lg-12 col-md-12 col-sm-12 col-xs-12 col-h"
pnl.Controls.Add(dCtrl)
If title <> "" Then pnl.Controls.Add(lbl) : pnl.Controls.Add(New LiteralControl("<br/>"))
pnl.Controls.Add(dCtrl)
' ///////////////////////////////////////////////////
pnlForm.Controls.Add(pnl)
'If title <> "" Then pnlForm.Controls.Add(lbl) : pnlForm.Controls.Add(New LiteralControl("<br/>"))
'pnlForm.Controls.Add(dCtrl)
Else
ConsoleLogJS(Me.Page, "(LoadFormConstruct) " & "(" & id & ")" & title & " : IS NOTHING !!!", "x")
End If
Else
ConsoleLogJS(Me.Page, "(LoadFormConstruct) " & "(" & id & ")" & title & " : NO TYPE !!!", "x")
End If
Next
Else
ConsoleLogJS(Me.Page, "(LoadFormConstruct) " & "DATAS_INPUT : EMPTY !!!", "x")
End If
Catch ex As Exception
ConsoleLogJS(Me.Page, ex.ToString, "x")
End Try
End Sub
#End Region
#Region "FAKE_DATAS"
Private Function GetListOfInput() As DataTable
Dim output As New DataTable
Try
output = GetTblInput()
Dim id As Integer = 0
Dim i1 As Integer = GetRandomInt(1, 4)
For i = 0 To i1
id += 1
output.Rows.Add("dInputTbx" & id, "tbx", "tbx" & i.ToString, "", "", True, "", "", "")
Next
Dim i2 As Integer = GetRandomInt(2, 4)
For i = 0 To i2
id += 1
output.Rows.Add("dInputChk" & id, "chk", "chk" & i.ToString, "", "", True, "", "", "")
Next
Dim i3 As Integer = GetRandomInt(1, 2)
For i = 0 To i3
id += 1
output.Rows.Add("dInputTxt" & id, "txt", "txt" & i.ToString, "", "", True, "", "", "")
Next
Dim i4 As Integer = GetRandomInt(2, 4)
For i = 0 To i4
id += 1
output.Rows.Add("dInputRbl" & id, "rbt", "rbt" & i.ToString, "", "", True, "rbt", "", "")
Next
Catch ex As Exception
End Try
Return output
End Function
Private Function GetTblInput() As DataTable
Dim output As New DataTable
output.Columns.Add("idDT", GetType(String))
output.Columns.Add("typeDT", GetType(String))
output.Columns.Add("titleDT", GetType(String))
output.Columns.Add("valueDT", GetType(String))
output.Columns.Add("tooltipDT", GetType(String))
output.Columns.Add("enabledDT", GetType(Boolean))
output.Columns.Add("groupNameDT", GetType(String))
output.Columns.Add("cssClassDT", GetType(String))
output.Columns.Add("cssStyleDT", GetType(String))
Return output
End Function
Private nbColTblRslt As Integer = 50
Private Function GetTblRslt() As DataTable
Dim output As New DataTable
For i = 0 To nbColTblRslt
output.Columns.Add("idInput" & i.ToString & "DT", GetType(String))
output.Columns.Add("valueInput" & i.ToString & "DT", GetType(String))
Next
Return output
End Function
#End Region
#Region "ConsoleLog"
Private Sub ConsoleLogJS(ByVal pPage As Page, ByVal pMsg As String, Optional ByVal pType As String = "", Optional ByVal pMethode As String = "")
If pType = "e" Or pType = "event" Then
pType = "[EVENT] : "
ElseIf pType = "x" Or pType = "err" Then
pType = "[ERROR] : "
ElseIf pType = "d" Or pType = "dbg" Then
pType = "[DEBUG] : "
ElseIf pType = "c" Or pType = "call" Then
pType = "[CALL] : "
ElseIf pType = "w" Or pType = "warn" Then
pType = "[WARNING] : "
End If
If pMsg <> "" Then
If (LCase(pMethode) = LCase("PageLoadEvent")) Then
ScriptManager.RegisterOnSubmitStatement(pPage, pPage.GetType(), Guid.NewGuid().ToString, "console.log('" & pType & HttpContext.Current.Server.HtmlEncode(ToolsText.FormatTextDisplayMsg(pMsg)) & "');")
ElseIf (LCase(pMethode) = LCase("ClickEvent")) Then
ScriptManager.RegisterClientScriptBlock(pPage, pPage.GetType(), Guid.NewGuid().ToString, "console.log('" & pType & HttpContext.Current.Server.HtmlEncode(ToolsText.FormatTextDisplayMsg(pMsg)) & "');", True)
Else
ScriptManager.RegisterStartupScript(pPage, pPage.GetType(), Guid.NewGuid().ToString, "console.log('" & pType & HttpContext.Current.Server.HtmlEncode(ToolsText.FormatTextDisplayMsg(pMsg)) & "');", True)
End If
End If
End Sub
#End Region
#Region "Tools"
Private Function GetRandomInt(ByVal Min As Integer, ByVal Max As Integer) As Integer
Dim Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
#End Region
End Class