Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 210137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:58:42+00:00 2026-05-11T17:58:42+00:00

I’m trying to create a function that searches up a WebControl’s parent-child relationship (basically

  • 0

I’m trying to create a function that searches up a WebControl’s parent-child relationship (basically the opposite of WebControl.FindControl(id as String), but looking for a specific WebControl type).

Example:
I have a user control in an ItemTemplate for a GridViewRow. I’m trying to reference the GridViewRow from the user control. The user control may or may not be inside a div or other types of controls, so I don’t know exactly how many parent’s up to look (i.e. I can’t just use userControl.Parent.Parent). I need a function that will find the first GridViewRow that it finds on the way up the parent-child hierarchy.

Thus the need for this function. Unless there’s a better way to do this?
Anyway, I want to the function I’m creating to be fairly generic, so that different WebControl types (i.e. GridViewRow, Panel, etc.) can be specified depending on what I’m looking for. Here’s the code I’ve written:

Public Function FindParentControlByType(ByRef childControl As WebControl, ByVal parentControlType As WebControl.Type, Optional ByRef levelsUp As Integer = Nothing) As WebControl
    Dim parentControl As WebControl = childControl
    Dim levelCount = 1
    Do While Not parentControl.GetType = parentControlType
        If Not levelsUp = Nothing AndAlso levelCount = levelsUp Then
            parentControl = Nothing
            Exit Do
        End If
        levelCount += 1
        parentControl = parentControl.Parent
    Loop
    parentControl.FindControl(
    Return parentControl
End Function

I know the “ByVal parentControlType as WebControl.Type” in the function definition won’t work — that’s what I’m looking for.

I’m sure there’s a better way to do this, so please feel free to make me look simple by point it out!

Thanks Guys!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:58:42+00:00Added an answer on May 11, 2026 at 5:58 pm

    you should be able to do this easily using recursion. here’s an example to get you started or possibly solve your problem.

    not exactly that great on VB syntax anymore, but i’m sure you could run this through a converter (like converter.telerik.com)

    C# code

    public T FindParentControl<T>(
        ref WebControl child, 
        int currentLevel, 
        int maxLevels)
        where T : WebControl
    {
        if (child.Parent == null || currentLevel > maxLevels)
            return null;
    
        if (child.Parent is T)
            return child.Parent as T;
        else 
            return FindParentControl<T>(
                child.Parent, 
                currentLevel + 1, 
                maxLevels);
    }
    

    VB.NET Code (by converter.telerik.com)

    Public Function FindParentControl(Of T As WebControl)(
        ByRef child As WebControl,
        currentLevel As Integer, 
        maxLevels As Integer) As T
    
        If child.Parent = Nothing OrElse currentLevel > maxLevels Then
            Return Nothing
        End If
    
        If TypeOf child.Parent Is T Then
            Return TryCast(child.Parent, T)
        Else
            Return FindParentControl(Of T)(child.Parent, currentLevel + 1, maxLevels)
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer def foo(arg) if arg.is_a?(Symbol) do_symbol_stuff else do_string_stuff end end May 12, 2026 at 3:19 pm
  • Editorial Team
    Editorial Team added an answer We recently deployed an app that received approx. 20 million… May 12, 2026 at 3:19 pm
  • Editorial Team
    Editorial Team added an answer You can use the label.Name property to change the name… May 12, 2026 at 3:19 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.