Copy all control values of web page to get set properties

This class is responsible for fetching control values from web page and copying into DTO's


Public Class CopyProperties

Public Shared Sub CopyFromForm(ByVal formName As String)

//formName is here the DTO class name which contains all properties of web page

Dim session As HttpSessionState = HttpContext.Current.Session
Dim request As HttpRequest = HttpContext.Current.Request

'Gets form keys
Dim Keys() As String = request.Form.AllKeys
Dim files As HttpFileCollection = request.Files


Dim actionDtoType As Type = Type.GetType(formName)
Dim actionFormAssembly As Assembly = Assembly.GetAssembly(actionDtoType)
Dim actionForm As ActionDTO = actionFormAssembly.CreateInstance(formName)
Dim commandForm As Type = actionForm.GetType()


Dim properties() As PropertyInfo = commandForm.GetProperties

'Copies form field values into corresponding dto file
If Not properties Is Nothing Then

Dim i As Integer = 0

Do While (i < Keys.Length)

Dim j As Integer = 0

Do While (j < properties.Length)

If Keys(i).StartsWith(NAPResource.ctl00,
StringComparison.CurrentCultureIgnoreCase) Then

Keys(i) = Keys(i).Substring((Keys(i).LastIndexOf(NAPResource.doller) + 4), Keys(i).Length - (Keys(i).LastIndexOf(NAPResource.doller) + 4))

End If

If Keys(i).Equals(properties(j).Name, StringComparison.CurrentCultureIgnoreCase) Then

Try

Dim value As Object = request.Form(i)
properties(j).SetValue(actionForm, Convert.ChangeType(value, properties(j).PropertyType), Nothing)
Exit Do

Catch argEx As ArgumentException
Throw argEx
Catch ex As Exception
Throw ex
End Try

End If
j = (j + 1)
Loop
i = (i + 1)
Loop

'Copies files of form into dto's
If Not files Is Nothing Then

For Each key As String In files.Keys

Dim k As Integer = 0

While (k < properties.Length)

If key.EndsWith(properties(k).Name, StringComparison.CurrentCultureIgnoreCase) Then

Try

If (properties(k).CanWrite AndAlso (Not (files(key)) Is Nothing)) Then
properties(k).SetValue(actionForm, Convert.ChangeType(files(key), properties(k).PropertyType), Nothing)
'TODO: Warning!!! break;If

End If

Catch argEx As ArgumentException
Throw argEx
Catch ex As Exception
Throw ex
End Try

End If
k = k + 1
End While
Next
End If
End If

'Store into the Session
session(formName) = actionForm

End Sub

End Class


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: