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

  • Home
  • SEARCH
  • 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 31709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:38:52+00:00 2026-05-10T13:38:52+00:00

Having a heckuva time with this one, though I feel I’m missing something obvious.

  • 0

Having a heckuva time with this one, though I feel I’m missing something obvious. I have a control that inherits from System.Web.UI.WebControls.Button, and then implements an interface that I have set up. So think…

public class Button : System.Web.UI.WebControls.Button, IMyButtonInterface { ... } 

In the codebehind of a page, I’d like to find all instances of this button from the ASPX. Because I don’t really know what the type is going to be, just the interface it implements, that’s all I have to go on when looping through the control tree. Thing is, I’ve never had to determine if an object uses an interface versus just testing its type. How can I loop through the control tree and yank anything that implements IMyButtonInterface in a clean way (Linq would be fine)?

Again, know it’s something obvious, but just now started using interfaces heavily and I can’t seem to focus my Google results enough to figure it out 🙂

Edit: GetType() returns the actual class, but doesn’t return the interface, so I can’t test on that (e.g., it’d return ‘MyNamespace.Button‘ instead of ‘IMyButtonInterface‘). In trying to use ‘as‘ or ‘is‘ in a recursive function, the type parameter doesn’t even get recognized within the function! It’s rather bizarre. So

if(ctrl.GetType() == typeToFind) //ok  if(ctrl is typeToFind) //typeToFind isn't recognized! eh? 

Definitely scratching my head over this one.

  • 1 1 Answer
  • 0 Views
  • 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. 2026-05-10T13:38:52+00:00Added an answer on May 10, 2026 at 1:38 pm

    Longhorn213 almost has the right answer, but as as Sean Chambers and bdukes say, you should use

    ctrl is IInterfaceToFind 

    instead of

    ctrl.GetType() == aTypeVariable   

    The reason why is that if you use .GetType() you will get the true type of an object, not necessarily what it can also be cast to in its inheritance/Interface implementation chain. Also, .GetType() will never return an abstract type/interface since you can’t new up an abstract type or interface. GetType() returns concrete types only.

    The reason this doesn’t work

    if(ctrl is typeToFind)   

    Is because the type of the variable typeToFind is actually System.RuntimeType, not the type you’ve set its value to. Example, if you set a string’s value to ‘foo‘, its type is still string not ‘foo‘. I hope that makes sense. It’s very easy to get confused when working with types. I’m chronically confused when working with them.

    The most import thing to note about longhorn213’s answer is that you have to use recursion or you may miss some of the controls on the page.

    Although we have a working solution here, I too would love to see if there is a more succinct way to do this with LINQ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looks like I was on the right track.. In fact,… May 11, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Generally, you should use require. load will re-load the code… May 11, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer I think your current idea would be the best option.… May 11, 2026 at 5:03 pm

Related Questions

Having a heckuva time with this one, though I feel I'm missing something obvious.
Having a problem getting a TreeView control to display node images. The code below
Having a vector containing pointers to objects then using the clear function doesn't call
Having a bit of trouble with the syntax where we want to call a
Having a problem trying to create a function, as part of a BizTalk helper

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.