将网络表单中编辑的数据获取到vb.net代码

问题描述 投票:0回答:1

此代码是否可以将 Web 表单中编辑的数据移动到 vb.net 代码并分配给控件。

我想使用 jQuery javascript 在 vb.net 中自动保存 Web 表单数据

Partial Class AccountSummary
    Inherits System.Web.UI.Page

    <WebMethod()>
    Public Shared Sub SaveData(editedFields As Object)
        Try
            ' Deserialize the JSON data
            Dim serializer As New JavaScriptSerializer()
            Dim editedFieldsDictionary As Dictionary(Of String, Object) = serializer.Deserialize(Of Dictionary(Of String, Object))(editedFields.ToString())

            ' Update controls on the server side using only edited fields
            For Each keyValuePair In editedFieldsDictionary
                Dim controlId As String = keyValuePair.Key
                Dim controlValue As Object = keyValuePair.Value

                ' Find the control by ID
                Dim control As Control = FindControlRecursive(Page, controlId)

                If control IsNot Nothing Then
                    ' Update the control value based on the type
                    If TypeOf control Is TextBox Then
                        DirectCast(control, TextBox).Text = controlValue.ToString()
                    ElseIf TypeOf control Is CheckBox Then
                        DirectCast(control, CheckBox).Checked = Convert.ToBoolean(controlValue)
                    ElseIf TypeOf control Is DropDownList Then
                        DirectCast(control, DropDownList).SelectedValue = controlValue.ToString()
                    End If
                End If
            Next
        Catch ex As Exception
            ' Handle exception
        End Try
    End Sub

    ' Recursive method to find a control by ID
    Private Shared Function FindControlRecursive(parent As Control, controlId As String) As Control
        For Each child As Control In parent.Controls
            If child.ID = controlId Then
                Return child
            End If

            Dim foundControl As Control = FindControlRecursive(child, controlId)
            If foundControl IsNot Nothing Then
                Return foundControl
            End If
        Next

        Return Nothing
    End Function
End Class
vb.net
1个回答
0
投票
Imports System.Web.Services
Imports System.Web.Script.Json
Imports System.Web.Script.Serialization

Partial Class AccountSummary
    Inherits System.Web.UI.Page

    
    Public Shared Sub SaveData(editedFields As Object)
        Try
            
            Dim serializer As New JavaScriptSerializer()
            Dim editedFieldsDictionary As Dictionary(Of String, Object) = serializer.Deserialize(Of Dictionary(Of String, Object))(editedFields.ToString())

            
            For Each keyValuePair In editedFieldsDictionary
                Dim controlId As String = keyValuePair.Key
                Dim controlValue As Object = keyValuePair.Value

                
                Dim control As Control = FindControlRecursive(Page, controlId)

                If control IsNot Nothing Then
                    
                    If TypeOf control Is TextBox Then
                        DirectCast(control, TextBox).Text = controlValue.ToString()
                    ElseIf TypeOf control Is CheckBox Then
                        DirectCast(control, CheckBox).Checked = Convert.ToBoolean(controlValue)
                    ElseIf TypeOf control Is DropDownList Then
                        DirectCast(control, DropDownList).SelectedValue = controlValue.ToString()
                    End If
                End If
            Next
        Catch ex As Exception
            
        End Try
    End Sub

    
    Private Shared Function FindControlRecursive(parent As Control, controlId As String) As Control
        For Each child As Control In parent.Controls
            If child.ID = controlId Then
                Return child
            End If

            Dim foundControl As Control = FindControlRecursive(child, controlId)
            If foundControl IsNot Nothing Then
                Return foundControl
            End If
        Next

        Return Nothing
    End Function
End Class
© www.soinside.com 2019 - 2024. All rights reserved.