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

J

jobs

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.
 
J

jobs

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.
 
B

Brandon Gano

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
 
J

jobs

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"



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
 
J

jobs

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.
 
J

jobs

For Each ctrl As Control In
whichformview.Controls(0).Controls(1).Controls
xxxx = ctrl.ClientID.ToString()
Next


This produces only one iteration.

ctl00_Content1_FormMaint_ctl01

I think the original code, which i constructed from pieces of example,
reqires that I find the control in one row and break it up into cells.
It would be great if I do another for loop inside that one rather
than:


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

Back on issue I think somehow the div tag is changing the indexes, but
no combination seems to work.

here's the markup:


<asp:FormView SkinID="retailer" ID="FormMaint"
runat="server" DefaultMode="Edit"
BackColor="Transparent" GridLines="Both" Width="400px"
DataSourceID="DSForm"
DataKeyNames="UserId"
OnItemCreated="Form_ItemCreated">
<EditItemTemplate>
<div id="formdiv">
<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"
<asp:Label ID="RoleLabel"
SkinID="Retailer" runat="server" Text="Role:" />
<br />
<asp:DropDownList
ID="RoleDropDown" SkinID="Retailer" runat="server"
DataValueField="RoleName"
DataTextField="RoleName"
SelectedValue='<%# Bind("RoleName") %>' DataSourceID="DSRoles" />

.... continues.


Thanks again.
 
J

jobs

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.
 
J

Jesse Houwing

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:
 

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