By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <cr****@earthlink.net> wrote in message news:e4**************************@posting.google.c om...
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

Nov 18 '05 #2

P: n/a
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!

"Thomas Dodds" <th*********@hotmail.com> wrote in message news:<uU**************@TK2MSFTNGP15.phx.gbl>...
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" <cr****@earthlink.net> wrote in message
news:e4**************************@posting.google.c om...
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

--

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.