Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placeholder control, changed in 2.0 ? 1

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
Im trying to update a page for an old website buit in .net 1.1

In the old site, I can access a Placeholder simply by typing
PlaceHolder p = (PlaceHolder)FindControl("CorrectPlaceholder");

However, on the new site, this doesnt work, and the FindControl() method returns null.

If I look into the page I can see the placeholder I want, but I end up having to go throuhg 3 or 4 FindControl() steps to be able to access it.

Can anyone tell me how to get round this issue, so I only need the one FindControl() statement ?

And also why this has changed?

K
 
FindControl finds the control within the current control. it doesn't (in most cases) search sub controls for the control.

you could use a recursive function.
Code:
public static class PageUtil
{
   public T FindNestedControl<T>(Control parent, string id) where T : Control
   {
      var control = parent.FindControl(id);
      if(control != null)
      var typed_control = control as T;
      if(control != null) return control;
      parent.Controls.First(c=>FindNextedControl<T>(c, id));
   }
}
from the page you can call
Code:
var textbox = PageUtil.FindNestedControl<TextBox>(this, "foo");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Not using 3.5, but I know what you are suggesting, thanks.

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top