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.com> wrote in message
news:1185058361.275606.250420@.g4g2000hsf.googlegroups.com...
> 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 /
>
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.com> wrote:
> 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.
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"
SkinID="Retailer" runat="server" Text='<%# Bind("UserName") %>' /><br /
>
<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.
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.

0 comments:

Post a Comment