WebLog of Ken Cox

Web Links

Request for a Better FindControl Method in ASP.NET

<rant>

I'm spending hours trying to get a reference to a FileUpload control that's inside an InsertTemplate which is inside a Panel which is inside a Listview which is inside a Panel in an ASP.NET ContentPlaceHolder.

It's crazy to have to figure out all these naming containers (and parents, grandchildren, cousins, and in-laws) manually when ASP.NET creates the control structure and knows perfectly well where things are and what they're called.

Therefore, it would be nice to have a better FindControl method that would automatically (using LINQ?) do the recursion to come up with the control reference. You'd just pass in the control ID as a string.

When I look at the browser DOM's document.getElementId("txtJLLAmt1")  I have to think this could be built into ASP.NET. Here's a post with the basic idea that the ASP.NET Team could build on:

http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx

</rant>

Thanks for listening. I feel better having ranted - even though I don't have the problem solved.

Ken

Comments

Request for a Better FindControl Method in ASP.NET - WebLog of Ken Cox said:

Pingback from  Request for a Better FindControl Method in ASP.NET - WebLog of Ken Cox

# March 19, 2008 6:57 PM

LaptopHeaven said:

# March 19, 2008 10:48 PM

Ken Cox [MVP] said:

Thanks for the link. It leads to other useful routines.

I still think this capability should be built into ASP.NET itself. The ASP.NET team could implement the most efficient solution and make it available for everyone.

Ken

# March 19, 2008 11:52 PM

LaptopHeaven said:

If your using .NET 3.5, you could use an extension method to handle this same functionality.

I find extension methods a huge addition to the framework in 3.5.

# March 20, 2008 12:20 AM

Ken Cox [MVP] said:

Thanks. I went with the LINQ solution based on the code in the blog I referenced. I call it like this:

FileUpload flup = ListView1.Controls.All().OfType<FileUpload>().FirstOrDefault();

It works fine and is quick to code, but I'm afraid a LINQ implementation might be overly resource-intenstive for a busy production site.

Ken

# March 20, 2008 12:29 AM

AndrewSeven said:

The recursive find, whatever way it is implemented only works when the control is relatively unique on the page.

If you have a repeater with TextBox1 in the item template, the recursive find may find the first one, but some other person drops another TextBox1 into a different naming container, the recursive find might find it instead.

I really think its better to be explicit about the control you need to find. If you do choose to implement a recursive find control, you can do it with an extension method thus avoiding the need to put it in the framework.

I think that if you want to get rid of all the visible use of find control one option is code generation. I built a control that you can drop on the page and use to generate another code-behind file. Given a page with GridView1 that has Textbox1 in the item template, the generated code allows things like: this.Controls.GridView1.Rows[desiredRowIndex].Item.Textbox1.Text = "bob"

# March 20, 2008 9:11 AM

Jeff said:

I just used Reflector to find the sealed version that does this, and it works like a champ. I forget where I found it, but I recall looking around validators or something, which have to find the control they validate by ID.

# March 20, 2008 10:35 AM

rascunho » Blog Archive » links for 2008-03-20 said:

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-03-20

# March 20, 2008 4:26 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)