Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jobs - 21 Jul 2007 22:52 GMT
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.
jobs - 21 Jul 2007 23:52 GMT
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.
Brandon Gano - 22 Jul 2007 00:37 GMT
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

> 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.
jobs - 22 Jul 2007 02:39 GMT
> 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.
jobs - 22 Jul 2007 02:53 GMT
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.
jobs - 22 Jul 2007 00:44 GMT
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
jobs - 22 Jul 2007 03:09 GMT
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.
Jesse Houwing - 22 Jul 2007 16:35 GMT
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:
> This code was working, but then stopped working. I don't think I
> completely understand it.
[quoted text clipped - 38 lines]
>
> Thanks for any help or information.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.