Asp.net 动态表单数据返回
从动态表单添加。 填写完我的表格后,当您单击按钮“btnSubmit”时,它返回给我面板“pnlForm”中没有控制器;“pnlForm.Controls.Count”第37行返回0 这就好像Page.PostBack是在btnSubmit之前完成的。单击?!并且“Viewstate”仅在第二次单击后显示在“litRsltDebug”中Asp.net 动态表单数据返回,asp.net,vb.net,Asp.net,Vb.net,从动态表单添加。 填写完我的表格后,当您单击按钮“btnSubmit”时,它返回给我面板“pnlForm”中没有控制器;“pnlForm.Controls.Count”第37行返回0 这就好像Page.PostBack是在btnSubmit之前完成的。单击?!并且“Viewstate”仅在第二次单击后显示在“litRsltDebug”中 <section> <article class="container full"
<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>
导入Newtonsoft.Json
部分类页面\u动态表单
继承System.Web.UI.Page
私有BagDatasFormRlst As String=“”
私有BagDatasFormDebug As String=“”
私有子页\u DynamicForm\u Load(发送者作为对象,e作为事件参数)处理Me.Load
如果不是Page.IsPostBack,则
LoadFormConstruct()
ViewState(“BagDatasFormRlst”)=BagDatasFormRlst
ViewState(“BagDatasFormDebug”)=BagDatasFormDebug
其他的
BagDatasFormRlst=视图状态(“BagDatasFormRlst”)
BagDatasFormDebug=ViewState(“BagDatasFormDebug”)
如果结束
litRsltDebug.Text=bagdasformdebug&“
”&bagdasformrlst
如果为“BagDatasFormRlst”,则为ViewRslt(BagDatasFormRlst)
端接头
私有子btnSubmit\u单击(发送者作为对象,e作为事件参数)处理btnSubmit。单击
Dim debug As String=Now.ToString&“
”
将DT_Rslt标注为新数据表
尝试
DT_Rslt=GetTblRslt()
特例
ConsoleLogJS(Me.Page,“(btnSubmit_Click)”和“LOAD DT_RSLT:”&ex.ToString,“x”)
调试&=“(btnSubmit_Click)”和“LOAD DT_RSLT:”&ex.ToString&“
”
结束尝试
尝试
如果(DT_Rslt不是什么)那么
Dim数据作为新字典(字符串、字符串)
ConsoleLogJS(Me.Page,“ctrl.count:&pnlForm.Controls.count,“x”)
对于pnlForm.Controls中的每个ctrl As控件
调试&=“ctrl:”&ctrl.ID
ConsoleLogJS(Me.Page,“ctrl:”&ctrl.ID,“x”)
如果ctrl的类型为System.Web.UI.WebControl.RadioButton,则
将rbt设置为System.Web.UI.WebControl.RadioButton=DirectCast(ctrl,System.Web.UI.WebControl.RadioButton)
添加数据(rbt.ID,rbt.Checked)
如果结束
如果ctrl的类型为System.Web.UI.WebControl.CheckBox,则
将chk设置为System.Web.UI.WebControls.CheckBox=DirectCast(ctrl,System.Web.UI.WebControls.CheckBox)
数据添加(chk.ID,chk.Checked)
如果结束
如果ctrl的类型为System.Web.UI.WebControl.TextBox,则
将tbx设置为System.Web.UI.WebControls.TextBox=DirectCast(ctrl,System.Web.UI.WebControls.TextBox)
添加数据(tbx.ID,tbx.Text)
如果结束
下一个
' ///////////////////////////
如果datas.Count>0,则
尺寸i为整数=0
对于每个kvp,作为数据中的KeyValuePair(字符串、字符串)
尺寸k为字符串=kvp.Key
尺寸v为字符串=kvp.值
DT_Rslt.Rows(0)(“idInput”&i.ToString&“DT”)=kvp.Key
DT_Rslt.行(0)(“valueInput”和i.ToString&“DT”)=kvp.值
i+=0
下一个
其他的
ConsoleLogJS(Me.Page,“(btnSubmit_Click)”、“数据:非数据!!!”、“x”)
调试&=“(btnSubmit_Click)”&“数据:非数据!!!”&“
”
如果结束
BagDatasFormRlst=JsonConvert.SerializeObject(DT_Rslt)
其他的
ConsoleLogJS(Me.Page,“(btnSubmit_Click)”和“DT_RSLT:不存在!!!”,“x”)
调试(&=“(btnSubmit_Click)”和“DT_RSLT:不存在!!!”&“
”
如果结束
特例
ConsoleLogJS(Me.Page,“(btnSubmit_Click)”和“Try Form:”&ex.ToString,“x”)
调试(=“(btnSubmit_Click)”和“Try Form:&ex.ToString&“
”
结束尝试
BagDatasFormDebug=调试
ViewState(“BagDatasFormRlst”)=BagDatasFormRlst
ViewState(“BagDatasFormDebug”)=BagDatasFormDebug
端接头
#区域“ViewRslt”
私有子视图RSLT(数据作为字符串)
如果数据为“”,则
ConsoleLogJS(Me.Page,“(ViewRslt)”和数据,“x”)
将输出变暗为字符串=“”
尝试
将DT_Rslt标注为新数据表
尝试
DT_Rslt=GetTblRslt()
DT_Rslt=JsonConvert.DeserializeObject(数据表的)(数据)
特例
ConsoleLogJS(Me.Page,“(btnSubmit_Click)”和“LOAD DT_RSLT:”&ex.ToString,“x”)
结束尝试
如果DT_Rslt.Rows.Count>0,则
Dim itemId为String=“”,itemValue为String=“”
对于i=0到DT_Rslt.Rows.Count-1
对于j=0至nbColTblRslt
itemId=CStr(第(i)行(“idInput”和j.ToString&“DT”))
itemValue=CStr(第(i)行(“valueInput”和j.ToString&“DT”))
输出&=itemId&“:”&itemValue&“;”&“
”
下一个
下一个
litRslt.Text=输出
其他的
ConsoleLogJS(Me.Page,“(ViewRslt)”和“非RSLT!”,“dbg”)
如果结束
特例
ConsoleLogJS(Me.Page,“(ViewRslt)”和ex.ToString,“x”)
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