p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: Dynamic casting of web controls


Message #1 by "Larkin Young" <youngld@j...> on Mon, 20 Jan 2003 01:43:17
I implemented a technique for page templates in asp.net, shown in an 
article here: http://www.csharphelp.com/archives2/archive383.html. It 
involves building a user control as the template control, with 
placeholder controls that are used to add the body, main content, etc to 
the template base class. The problem, more of an annoyance really, is 
that in the templated class, in order for the templated page's codebehind 
to recognize the controls you have to do something like: 

this.PasswordField = (TextBox)this.template.FindControl("PasswordField");

--this is ok for a templated page that only has a few controls, but for 
say a member profile page that has 30 different textboxes and form 
validator controls on it, this technique can get extremely tedious. The 
main point of this message is to ask if it is possible to do dynamic 
casting, so for instance, instead of using the above line for every 
individual control on the page i need to reference, i can enumerate 
through every control on the page and call the FindControl method on each 
one, and casting it to the correct type for each one. I tried:

foreach(Control pageControl in this.Controls)
{
    pageControl = (pageControl.GetType())this.template.FindControl
(pageControl.ID);
}
but the casting operator can't evaluate the Type object in the casting 
expression...anyone have any idea how this might be accomplished, how one 
could cast to the correct type when the exact type is not known until 
runtime?

  Return to Index