Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 25th, 2005, 09:47 AM
Authorized User
 
Join Date: Sep 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding child controls

I have a page with a panel and several buttons. Clicking the buttons loads specific user controls(.ascx files) into the panel. That works fine. Those user controls contain text boxes. I want to save the values entered into those boxes. But I'm having trouble getting their values. The following code is what I have in my "save" button for testing, but it gets me nothing:

Code:
        For Each myControl As Control In Page.Controls 'All controls on the page
            For Each myControl2 As Control In myControl.Controls 'One of these should be my panel
                For Each mycontrol3 As Control In myControl2.Controls 'The only control in my panel is my user control
                    For Each mycontrol4 As Control In mycontrol3.Controls 'There should be text boxes in this collection
                        If TypeOf mycontrol3 Is TextBox Then
                            Label1.Text += CType(myControl2, TextBox).Text & ","
                        End If
                    Next
                Next
            Next
        Next
__________________
-Hunter
 
Old October 25th, 2005, 02:35 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Is the Save button on the Page or on the User Control. If it is on the UserControl, it should be no problem.

 
Old October 25th, 2005, 03:08 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

If you have the save button on the page you need to do a few things:

'Assuming your user control is named WebUserConrol1 put this line at the page level.
Protected WithEvents WebUserControl11 As WebUserControl1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Call a subroutine with the user control you just declared.
        IterateThroughChildren(WebUserControl11)
    End Sub

   Private Sub IterateThroughChildren(ByVal parent As Control)

        Dim c As Control
        For Each c In parent.Controls
            If TypeOf c Is TextBox Then
                Dim tb As TextBox
                tb = CType(c, TextBox)
                Response.Write(tb.Text) '' do something with the value.

                If c.Controls.Count > 0 Then
                    IterateThroughChildren(c)
                End If

            End If

        Next c

    End Sub

 
Old October 27th, 2005, 08:18 AM
Authorized User
 
Join Date: Sep 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. What I'm actually trying to do now, is have the save event in the user control itself. Now I need to be able to call that Public Sub Save_Form() from the main page. Any ideas on how to do this?
 
Old October 27th, 2005, 09:09 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Why do you need to call it from the page if you are placing the code and button on the user control?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the first child element humansky XSLT 5 April 17th, 2008 08:46 AM
Finding Controls and Checking Boxes rstelma ASP.NET 1.0 and 1.1 Professional 1 October 30th, 2007 02:01 PM
Accessing Child Controls of Nested Repeater... Jayahar ASP.NET 2.0 Professional 3 October 11th, 2007 01:24 PM
Accessing the MDI form controls from child forms krishnasamaga Visual Studio 2005 1 June 7th, 2006 08:09 AM
Finding Dynamic controls mpdillon General .NET 0 March 30th, 2005 01:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.