1

I'm finding a dropdown in an ascxcontrol on my aspx in the following way.

Dim cp As ContentPlaceHolder = DirectCast(Page.Form.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Dim ascx As UserControl = DirectCast(cp.FindControl("drpType"), UserControl)
Dim drpType As DropDownList = DirectCast(ascx.FindControl("drpType"), DropDownList)

Is there a faster way without having to acces all the elements on the page?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Arno
  • 55
  • 7

3 Answers3

0

If you know what naming containing the control is in you can go

ucNamingContainerControl.FindControl(controlId)

That will at least limit it to that section of the page.

Otherwise the only other thing I can think of is if you are accessing a predefined set of controls - put them in a Dictionary collection and use the Find method to pick them out. Could be a quicker retrieval but might look a bit clunky on the page.

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
0

I wouldn't try and reference a control within a user control this way, the user control should encapsulate these and the page should talk to public properties.

PMC
  • 4,698
  • 3
  • 37
  • 57
0

Depends on what you're trying to do.

Without a great deal of context, I can only assume that you are either getting or setting the value of the dropdown.

I wouldn't use the approach that you're going for. It introduces an element of implementation specific coupling.

You would be far better off exposing whatever you need to get/set via a property which you can call from the .aspx page.

However, in answer to your question, if you are going to reference the dropdown from the .aspx page, you will have to use FindControl.

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
  • I need the same dropdown on every page. But dropdown changes over projects (less or more options) so with a user control I only have to edit the control once instead of editing it in every page...Is there a better way then? – Arno Dec 02 '11 at 11:11