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

C

crjunk

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
 
T

Thomas Dodds

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
 
C

crjunk

Thanks for your suggestion. I ended up figuring out that I needed to
change my For Each statement. Here is what I did to correct the
problem:

Dim ctrl As Control
Dim ctlNameText As TextBox

For Each ctrl In Page.FindControl("Form1").Controls
If TypeOf (ctrl) Is TextBox Then
ctlNameText = ctrl
If (ctlNameText.Enabled = True) And (ctlNameText.Visible =
True) Then
Dim FieldName As String
FieldName = ctrl.ID
Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
ctlNameText.Text.Trim.ToUpper + ""
End If
End If
Next

Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top