I'm fairly new to .net (especially 2.0). I using vb.net, 2.0 framework and
Visual Studio 2005.
I'm trying to create a class with a member that will iterate through a web
page and discover all the controls on the page, list their types, names and
what value they hold. What I want is to be able to save all the fields on
any page without having to know what fields there are to begin with. I'll
then save the fields and values to a collection for later retrieval.
Ultimately I want to create a class that can iterate through all the pages
on the site calling the page class for each page so all the controls on the
entire site can be saved and retrieved.
This will allow me to, for instance, to set the value of a contol three
pages back without having to pass variables or get/set a bunch of sessions.
I will also allow me to enable/disable controls site wide or page wide from
anywhere on the site.
When I attempt to 'discover' what controls are on a given page I have
managed to determine the control types but not their names.
Can anyone provide an example or point me to where I might find one?
Thanks!
' loop through the controls on the page.
Dim c As Control
Dim child As Control
Dim txtbox As TextBox
Dim ddl As DropDownList
Dim returnvalue As Type
For Each c In page.Controls
If c.Controls.Count > 0 And c.Controls.GetType.ToString =
"System.web.ui.HtmlControls.htmlform" Then
For Each child In c.Controls
If child.GetType.ToString = "System.web.ui.webcontrols.textbox" Then
txtbox = CType(child, TextBox)
' no blanks allowed.
If txtbox.Text.Equals("") Then
txtbox.BackColor = Color.Magenta
txtbox.Text = "required."
End If
End If
If child.GetType.ToString =
"System.web.ui.webcontrols.dropdownList" Then
ddl = CType(child, DropDownList)
' no blanks allowed.
If ddl.SelectedValue.Equals("") Then
ddl.BackColor = Magenta
End If
End If
Next
End If
Next
Return bRet
End Function
In the webform or webform codebehind page in the submit click event enter
something like this:
If myClass.Validation(Me)=True Then
' No Error call save Function
Else
' An Error has occurred return To form displaying list of errors
End If