Re: Save Record With "For Each ctrl In Me.controls" Statement

From: Thomas Dodds (thomasdodds_at_hotmail.com)
Date: 08/31/04


Date: Tue, 31 Aug 2004 16:43:47 -0400

make it a sub as you need to call it recursively as the form is a child control in the collection which has it's own collection of controls (preumably where your textboxes are) ...

I use this (modify it as needed):

Private Sub IterateThroughChildren(ByVal parent As Control)
    Dim c As Control
    Dim lb As LinkButton
    Dim strToolTip As String = "Click to Save all data and proceed to this section."
    Dim strConfirmText As String = "Do you wish to save your data first?"

    For Each c In parent.Controls
        If InStr(LCase(c.GetType.ToString), "linkbutton") Then
            lb = CType(c, LinkButton)
            If intContID = 0 Then
                lb.Enabled = False
             Else
                lb.ToolTip = strToolTip
                lb.Attributes.Add("onclick", "javascript: doSaveFirst('" & lb.ID & "','" & strConfirmText & "');")
                lb.CommandName = "true"
                lb.CausesValidation = False
            End If
        End If

        If c.Controls.Count > 0 Then
            IterateThroughChildren(c)
        End If
    Next c
End Sub

"crjunk" <crjunk@earthlink.net> wrote in message news:e45e90aa.0408311201.55e11f7f@posting.google.com...
> I'm trying to write a piece of code that will programatically save a
> record automatically without me having to add a new ' Row.Item("ADD1")
> = txtAdd1.Text.Trim.ToUpper ' type command each time I add a new
> textbox. I've named all my textboxes in the following format txtAdd1
> and I've named all my field in the SQL table the same way minus the
> txt at the beginning.
>
> This code scans the form for textboxes. If it finds a text box that
> is not disabled, then it is supposed to save the text, typed by the
> user, into the SQL table. The only problem is that the text the user
> typed in is not being saved to the table, such as "John" in my
> FirstName field.
>
> I tried adding a label (Label2) to my form so I could see what might
> be going on, but no text is displayed in Label2 after the postback.
>
> Does anyone have any suggestions?
>
> Thanks,
> crjunk
>
> Here is what I have so far:
>
> Dim ctrl As Control
> Dim ctlNameText As TextBox
> Dim ctlNameDDL As DropDownList
>
> For Each ctrl In Me.Controls
> If (TypeOf (ctrl) Is TextBox) Then
> ctlNameText = ctrl
> If ctlNameText.Enabled = True Then
> Dim FieldName As String
> FieldName = ctrl.ID
> Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
> ctlNameText.Text.Trim.ToUpper + ""
> Label2.Text = Label2.Text.Trim + " " +
> ctlNameText.Text.Trim.ToUpper
> End If
> End If
> Next