Q. Smart Collection of Data on a Form

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I am looking for a smarter method of collecting data from a webform. The form I am currently working on only has 15 different variables to collect from, but a form could forseeably have many more.

Currently, to collect form data, I am stuck writing something like this:
Visual Basic:
[font=Courier New][color=black]Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles[/color][/font][font=Courier New][color=black] btnSubmit.Click
[/color][/font][font=Courier New][color=black]    Dim strCollection As String
[/color][/font][font=Courier New][color=black]    strCollection = txtName.Text
    strCollection &= txtAddress.Text
    strCollection &= chkHasInternet.Checked.ToString[/color][/font]
[font=Courier New][color=black]    ' // etc.
[/color][/font][font=Courier New][color=black]End [/color][/font][color=#0000ff][font=Courier New][color=black]Sub[/color][/font][color=black]
[/color][/color]
Like I said: That works, but a "smarter way" would be something like this:
Visual Basic:
[font=Courier New][color=black]Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles[/color][/font][font=Courier New][color=black] btnSubmit.Click[/color][/font]
[font=Courier New][color=black]    Dim objVariable as Object
[/color][/font][font=Courier New][color=black]    Dim strCollection As String[/color][/font]
[font=Courier New][color=black]    strCollection = ""
[/color][/font][font=Courier New][color=black]    For Each objVariable In Me.Controls[/color][/font]
[font=Courier New]        strCollection &= objVariable.ToString & vbCrLf[/font]
[font=Courier New]    Next[/font][color=black]
[/color][font=Courier New][color=black]End [/color][/font][color=#0000ff][font=Courier New][color=black]Sub[/color][/font][color=black]
However, the "smarter way" works too well: It seems to be returning to me the string value of everything on the webpage. Is there a way to tell my code that I only want to collect my data from the input fields?

Also, I would appreciate any suggestions on the best way to handle large collections of data such as this. My "smarter way" is probably not the best way.

Thanks in advance for your help,
Joe[/color][/color]
 
In your loop you can check for TypeOf,
Also, if you have containers such as Panels, you can loop just those containers you want.
 
Boy, that's embarrassing....

I tried entering the following code for collecting my data:
Visual Basic:
[font=Fixedsys][color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnSubmit_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color][/font][font=Fixedsys] btnSubmit.Click[/font]
[font=Fixedsys][color=#0000ff][color=#008000]  [/color]Dim[/color] objCtrl [color=#0000ff]As[/color] [color=#0000ff]Object[/color][/font]
[font=Fixedsys][color=#0000ff][color=#008000]  [/color]Dim[/color] strInfo [color=#0000ff]As[/color] [color=#0000ff]String[/color][/font]
[font=Fixedsys][color=#008000]  [/color]strInfo = ""[/font]
[font=Fixedsys][color=#0000ff][color=#008000]  [/color]For[/color] [color=#0000ff]Each[/color] objCtrl [color=#0000ff]In[/color] [color=#0000ff]Me[/color][/font][font=Fixedsys].Controls[/font]
[font=Fixedsys][color=#0000ff][color=#008000]    [/color]Select[/color] [color=#0000ff]Case[/color][/font][font=Fixedsys] (objCtrl.GetType.Name)[/font]
[font=Fixedsys][color=#0000ff][color=#008000]      [/color]Case[/color][/font][font=Fixedsys] "RadioButton"[/font]
[font=Fixedsys][color=#008000]        [/color]strInfo &= objCtrl.checked.ToString & vbCrLf[/font]
[font=Fixedsys][color=#0000ff][color=#008000]      [/color]Case[/color][/font][font=Fixedsys] "DropDownList"[/font]
[font=Fixedsys][color=#008000]        [/color]strInfo &= objCtrl.SelectedValue.ToString & vbCrLf[/font]
[font=Fixedsys][color=#0000ff][color=#008000]      [/color]Case[/color][/font][font=Fixedsys] "CheckBox"[/font]
[font=Fixedsys][color=#008000]        [/color]strInfo &= objCtrl.Checked.ToString & vbCrLf[/font]
[font=Fixedsys][color=#0000ff][color=#008000]      [/color]Case[/color][/font][font=Fixedsys] "ListBox"[/font]
[font=Fixedsys][color=#0000ff][color=#008000]        [/color]Dim[/color] objValue [color=#0000ff]As[/color] [color=#0000ff]Object[/color][/font]
[font=Fixedsys][color=#0000ff][color=#008000]        [/color]For[/color] [color=#0000ff]Each[/color] objValue [color=#0000ff]In[/color][/font][font=Fixedsys] objCtrl.selectedValue[/font]
[font=Fixedsys][color=#008000]          [/color]strInfo &= objValue.SelectedValue.ToString & vbCrLf[/font]
[color=#0000ff][font=Fixedsys][color=#008000]        [/color]Next[/font][/color]
[font=Fixedsys][color=#0000ff][color=#008000]      [/color]Case[/color][/font][font=Fixedsys] "TextBox"[/font]
[font=Fixedsys][color=#008000]        [/color]strInfo &= objCtrl.text & vbCrLf[/font]
[font=Fixedsys][color=#0000ff][color=#008000]    [/color]End[/color] [color=#0000ff]Select[/color][/font]
[color=#0000ff][font=Fixedsys][color=#008000]  [/color]Next[/font][/color]
[font=Fixedsys][color=#0000ff]End[/color] [/font][color=#0000ff][font=Fixedsys]Sub[/font][/color]
But my output was no where near what I as expecting. In the "Immediate Window" I got this:
Code:
[font=Fixedsys]?objCtrl.GetType.FullName[/font]
[font=Fixedsys]"System.Web.UI.ResourceBasedLiteralControl"[/font]
[font=Fixedsys]?objCtrl.GetType.FullName[/font]
[font=Fixedsys]"System.Web.UI.HtmlControls.HtmlForm"[/font]
[font=Fixedsys]?objCtrl.GetType.FullName[/font]
[font=Fixedsys]"System.Web.UI.LiteralControl"[/font]
Have I done something extremely wrong?

Also, why isn't my VB code posting correctly? I am placing it within ["VB"] tags, but when I cut and past from VS.NET, it originally populates the text with Size=1, Face=Verdana. Is there a setting I am missing somewhere?
 
The problem is that an ASP.Net page isn't as simple to navigate through code as a Windows Forms based system. The HTML that is generated contains 3 controls: the HEAD bit of a page (all the <HTML>, <HEAD> etc), the <FORM>...</FORM> bit in the middle that contains the real set of controls and the footer bit </HTML> etc.

You are probably going to need to recurse through each control to see if it contains child controls
Visual Basic:
Public Sub LoopThroughControls(parentControl as Control)
Dim ctrl As Control
    ctrls  = New ArrayList
    For each ctrl in parentControl .Controls 
           'your code here
           Select Case (objCtrl.GetType.Name)        
           '....
           '..... 
    If ctrl .HasControls Then
            LoopThroughControls(ctrl )
        End If
    Next
 
Back
Top