Solution to the FindControl problem

Rate It (5)

Last post 04-29-2008 5:52 PM by CaptainStarbuck. 26 replies.

Sort Posts:

  • Re: Solution to the FindControl problem

    11-20-2007, 7:32 PM
    • Loading...
    • muybn
    • Joined on 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 456

    I receive this error message while trying to run this procedure: "The type or namespace name 'Generic' does not exist in the class or namespace 'System.Collections' (are you missing an assembly reference?)"

     

  • Re: Solution to the FindControl problem

    12-21-2007, 11:41 AM
    • Loading...
    • art03
    • Joined on 12-21-2007, 3:25 PM
    • Posts 1

    I get a null reference exception for this line:

     ctl = ctls.First.Value;

     When I try to use that code. Any ideas why? Thanks.
     

  • Re: Solution to the FindControl problem

    12-21-2007, 5:40 PM
    • Loading...
    • muybn
    • Joined on 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 456

    I've been through something similar a few times myself so I can probably help you. Before I try that, however, please give me the following information so I can customize an answer to your question:

    (1) VB or C#?   (2) The code for your control and what you want to find.

  • Re: Solution to the FindControl problem

    01-09-2008, 7:33 AM

    Nice Answer For the Find Control Problem

    Filed under:
  • Re: Solution to the FindControl problem

    01-16-2008, 4:39 AM
    • Loading...
    • ppp
    • Joined on 01-16-2008, 9:36 AM
    • Posts 1

    good job

     

     

  • Re: Solution to the FindControl problem

    02-26-2008, 1:41 PM
    • Loading...
    • IRumsey
    • Joined on 02-26-2008, 6:40 PM
    • Posts 1

    A couple more variations that may help you.  They both use generics to return a specific type of control.

     

    'Find first instance of a control:
    Private Shared Function FindControlIterative(Of T As Control)(ByVal root As Control, ByVal id As String) As T
        Dim ctl As Control = root
        Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)
    
        Do While (ctl IsNot Nothing)
            If ctl.ID = id Then
                Return ctl
            End If
            For Each child As Control In ctl.Controls
                If child.ID = id Then
                    Return child
                End If
                If child.HasControls() Then
                    ctls.AddLast(child)
                End If
            Next
            ctl = ctls.First.Value
            ctls.Remove(ctl)
        Loop
    
        Return Nothing
    End Function
    
    'Build collection of controls with matching ID (useful if root is a GridView control for example):
    Private Shared Function FindControlCollection(Of T As Control)(ByVal root As Control, ByVal id As String) As List(Of T)
        Dim ctl As Control = root
        Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)
        Dim ControlCollection As New List(Of T)
    
        Do While (ctl IsNot Nothing)
            If ctl.ID = id Then
                ControlCollection.Add(ctl)
            End If
            For Each child As Control In ctl.Controls
                If child.ID = id Then
                    ControlCollection.Add(child)
                End If
                If child.HasControls() Then
                    ctls.AddLast(child)
                End If
            Next
            If (ctls.Count > 0) Then
                ctl = ctls.First.Value
                ctls.Remove(ctl)
            Else
                ctl = Nothing
            End If
        Loop
    
        Return ControlCollection
    End Function
     
  • Re: Solution to the FindControl problem

    02-26-2008, 8:44 PM
    • Loading...
    • ks2007
    • Joined on 09-24-2007, 9:14 PM
    • Posts 221

    Could you please provide a C# version of the code since I am not familiar with VB?

    Thanks.

    If this answered your question, be sure to mark it as the answer.
  • Re: Solution to the FindControl problem

    02-26-2008, 11:00 PM
    • Loading...
    • muybn
    • Joined on 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 456

    Are you working in VB or C#? Also, write what you've tried to do so far so I and other people can get an idea of what you need and the names of some of your variables and controls. In this way, the response you receive can be more personalized to your needs.

    The whole idea is that when you go looking for Bill or John or Dave in a college dorm hall, you will get so many answers as to confuse you. You want to compartmentalize your search on a per-room basis in the dorm. Applying this to a Visual Studio project, you can more or less give the same name to various child controls (such as textboxes, buttons, etc.) contained in different parent controls (such as a CreateUserWizard, DetailsView, GridView, etc.), by knowing from which dorm room in the project you can find the control.

  • Re: Solution to the FindControl problem

    02-26-2008, 11:05 PM
    • Loading...
    • muybn
    • Joined on 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 456

    Excuse me, but everytime I look at this particular thread, it looks different. I wonder if a bunch of posts are being thrown into this thread mistakenly. I've seen it happen before.

  • Re: Solution to the FindControl problem

    04-11-2008, 12:12 PM
    • Loading...
    • ad5qa
    • Joined on 12-12-2006, 8:11 PM
    • Posts 23

    @david wendelken

    I like it when people break it down this way. You tend to get less questions. Anyway this method wont be used in every case but will go in my toolbox when needed. I will most likely use it every now and then.

     Thanks.

  • Re: Solution to the FindControl problem

    04-15-2008, 7:47 AM
    • Loading...
    • izeman
    • Joined on 10-08-2007, 8:07 PM
    • Sweden
    • Posts 82

     Hi ks2007

    It may be to late now but here it goes.

    You can write a helper class witch contains the FindControl method then in code behind you do the following:

     

    //Invoke a new instance of your helper class
    FindControls findControl = new FindControls();
    
    

        this assumes that your helper class is named FindControls.

    then you simply in stead of running FinControl() framework method you run following

     

    Label label= findControl.FindControlByID(Page.Controls, "ControlName") as Label;
     
    findControl = new instance of your helper class
    FindControlByID(Page.Controls, "ControlName") = the method in your helper class
    now you have a "new" label control in you code behind called label... you can use it like this. 
    label1.Text = label.Text; ...just like any other label control in your app.  
      
  • Re: Solution to the FindControl problem

    04-29-2008, 5:52 PM

    Unless you're creating controls dynamically that you need to find later, or you're drilling into a grid that's populated with a datasource, might it be useful to iterate once when a page is first built, and store some of that information in state? Where would the best place be to intercept the page build: Application_PreSendContent? Page_Unload? Has anyone done this?

Page 2 of 2 (27 items) < Previous 1 2