Thursday, March 22, 2012

Looping through a formview looking at all textboxes. use to work. now does not?

This code was working, but then stopped working. I don't think I
completely understand it.

I pass it a formview name and it would loop through checking the
value of textboxes.

problem is now as i debug it, it's telling me there are only a count
of 3 controls in that for loop. and they have clientid values like:

ctl00_Content1_FormMaint_ctl03

My thinking is that I'm at the wrong level, or maybe because I changed
the table organizing textboxes inside the formview that things are not
making sense.

suspect and confusing is this code which was once working..

whichformview.Controls(0).Controls(1),

What's weird is I have some 20 asp textboxes in that formview and it's
report tc.controls.count of only 3, the first being a literal which is
right, but the second coming back as type
System.Web.UI.HtmlControls.HtmlGenericControl which seems wrong.

Incidently, the forth control is a dropdownlist which I would have
just skipped over.

I've been using this same function in countless other aps. I did not
touch, but something in my asp.net is apparently through it off.

Function FormviewErrors(ByVal whichformview As FormView) As String
Dim Errors As String = Nothing
Dim fr As FormViewRow =
(CType(whichformview.Controls(0).Controls(1), FormViewRow))
Dim tc As TableCell = fr.Cells(0)
For i As Integer = tc.Controls.Count To 1 Step -1
Dim c As Control = tc.Controls(i - 1)
Dim x As String = c.ClientID.ToString
If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then

Thanks for any help or information.Some more info. The first control in my formview is not a literal,
it's a label. troubleshooting its saying only 3 controls under the
formview, when I have 20+ and the first is a literal, which it's not.

Thanks for any help or information.
What does the markup look like? It could be that you have moved the controls
inside another container control. A div, for example. This would cause your
code to return 1 HtmlGenericControl (div) instead of 20 TextBox controls.

If this is the case, you may want to consider using a recursive sub to
locate your TextBox controls. Something like (not tested):

For Each Control ctrl In whichformview.Controls(0).Controls(1).Controls
FindTextBoxes(ctrl)
Next

...

Public Sub FindTextBoxes(ByVal parent As Control)
If parent Is TextBox Then
' Handle text box items
Else
' Loop through child controls
For Each Control ctrl In parent.Controls
FindTextBoxes(ctrl)
Next
End If
End Sub

"jobs" <jobs@.webdos.comwrote in message
news:1185058361.275606.250420@.g4g2000hsf.googlegro ups.com...

Quote:

Originally Posted by

Some more info. The first control in my formview is not a literal,
it's a label. troubleshooting its saying only 3 controls under the
formview, when I have 20+ and the first is a literal, which it's not.
>
Thanks for any help or information.
>


Well, I've narrowed the issue down to having added a table inside my
formview. Somehow I must figure out how to navigate the formview
controls around a table, likely another child control or layer to
think about .. it boils down to this:

what does this table..

<table style="width: 625px">
<tr>
<td style="width: 309px; height:
214px" valign="top">
<asp:Label ID="UserNameLabel"
SkinID="Retailer" runat="server" Text="UserName:" /><br />
<asp:TextBox ID="UserName"
SkinID="Retailer" runat="server" Text='<%# Bind("UserName") %>' /><br /

Quote:

Originally Posted by

>


do to this ..(that use to work before it)

Dim fr As FormViewRow =
(CType(whichformview.Controls(0).Controls(1), FormViewRow))
Dim tc As TableCell = fr.Cells(0)
For i As Integer = tc.Controls.Count To 1 Step -1
On Jul 21, 7:37 pm, "Brandon Gano" <bg...@.inocompany.comwrote:

Quote:

Originally Posted by

For Each Control ctrl In whichformview.Controls(0).Controls(1).Controls
FindTextBoxes(ctrl)
Next


Thanks. I tried that but the problem persist.

I noticed however if i remove the DIV tag from around the table and
all my textbox that my original code works.
this works nice, but only if i remove the DIV tag around my textboxes.

For Each c As Control In
whichformview.Controls(0).Controls(1).Controls(0). Controls
If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then

Thanks.
Looks like they've got nested in either a table or a div or something
else that's also has a runat="server" on them.

To solve this you could use a simple recursive function (from teh top of
my mind, didn't try to compile it, so there might be a few syntax errors
in there):

// Pass your formview to this function as startHere
public void ForeachTextBoxIn(WebControl startHere)
{
foreach (WebControl wc in startHere.Controls)
{
TextBox tb = wc as TextBox;
if (tb != null)
{
//Do stuff with you textbox
}
else if (wc.HasChildControls)
{
ForeachTextBoxIn(wc);
}
}
}

This will work in every situation, and is better able to handle possible
changes in the future.

Jesse

* jobs wrote, On 21-7-2007 23:52:

Quote:

Originally Posted by

This code was working, but then stopped working. I don't think I
completely understand it.
>
I pass it a formview name and it would loop through checking the
value of textboxes.
>
problem is now as i debug it, it's telling me there are only a count
of 3 controls in that for loop. and they have clientid values like:
>
ctl00_Content1_FormMaint_ctl03
>
>
>
My thinking is that I'm at the wrong level, or maybe because I changed
the table organizing textboxes inside the formview that things are not
making sense.
>
suspect and confusing is this code which was once working..
>
>
whichformview.Controls(0).Controls(1),
>
What's weird is I have some 20 asp textboxes in that formview and it's
report tc.controls.count of only 3, the first being a literal which is
right, but the second coming back as type
System.Web.UI.HtmlControls.HtmlGenericControl which seems wrong.
>
>
>
Incidently, the forth control is a dropdownlist which I would have
just skipped over.
>
I've been using this same function in countless other aps. I did not
touch, but something in my asp.net is apparently through it off.
>
>
>
>
Function FormviewErrors(ByVal whichformview As FormView) As String
Dim Errors As String = Nothing
Dim fr As FormViewRow =
(CType(whichformview.Controls(0).Controls(1), FormViewRow))
Dim tc As TableCell = fr.Cells(0)
For i As Integer = tc.Controls.Count To 1 Step -1
Dim c As Control = tc.Controls(i - 1)
Dim x As String = c.ClientID.ToString
If c.GetType().ToString =
"System.Web.UI.WebControls.TextBox" Then
>
>
>
Thanks for any help or information.
>

0 comments:

Post a Comment