Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've inherited a project that has a custom Edit form for a list. It's called EditFormCustom.aspx. The page was customized through SharePoint designer. A code-behind was added by doing the following:

  • Add <%@ Page language="C#" Inherits="4part assembly name" %>
  • A custom class with the name that the page is inheriting, public partial class {classname} : WebPartPage
  • Deploy assembly to the GAC

This all works. I can step thru the code and everything is fine and dandy. What I'm having an issue with is programmatically locating controls that were added to the page in the designer.

This all worked when it was originally built for MOSS. It has since been upgraded to 2010, but the page cannot find the controls.

In addition to not finding the controls that previously existed (such as FormField), I added my own ASP.NET Label control. <asp:Label ID="lblFINDME" runat="server" />. Then from the code-behind:

System.Web.UI.WebControls.Label lblFINDME = (System.Web.UI.WebControls.Label)FindControlRecursive(this, "lblFINDME");

And the FindControlRecursive method:

public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;


    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }
    return null;
}

Is this not possible?

Edit 1

I am able to get a handle on some other controls, such as the master pages Placeholder controls.

Edit 2

I now added a sample label control within the PlaceHolderPageTitleInTitleArea control in the top of the page. I was able to get a handle on this control! In fact, I had intellisense in SPD when writing this control too. The control that I'm not able to find are within the PlaceHolderMain content place holder.

Edit 3

Very interesting. Just found that the control I'm trying to find is within a DataFormWebPart. I've never work with one of these before.

Edit 4

I've accepted an answer as it is specific to the question I asked. However, my problem was unrelated. Apparently when the transform of your xslt takes longer than 1 second, a stack overflow exception occurs. Because of this exception, my controls were not actually added to the page. I removed ~100 or so controls, so I only had 4 of them, 1 of them being the control I was trying to get a handle on. Sure enough, with only 4 controls to transform, it works without issue. Now I need to find a workaround.

share|improve this question
    
fwiw - this is a case-sensitive method. Do you have the ID typed exactly as it appears in the page? –  Dave Wise Nov 2 '11 at 13:50
    
Yup. Everything worked previously before the upgrade. –  Tim Gabrhel Nov 2 '11 at 13:57
    
What page event is the call to FindControlRecursive() in? –  Dave Wise Nov 2 '11 at 14:09
    
It was originally built with OnLoadComplete. I've since moved it to OnInit –  Tim Gabrhel Nov 2 '11 at 14:11
    
See my Edit 2 above –  Tim Gabrhel Nov 2 '11 at 14:14
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

Try placing the call to FindControlRecursive() in CreateChildControls() after the call to base.CreateChildControls() or in OnLoad(). For details on the page life cycle, check out this. You may also need to place a call to EnsureChildControls() prior to calling your method.

share|improve this answer
    
This does not seem to make a difference. –  Tim Gabrhel Nov 2 '11 at 14:42
    
Yeah, I posted that right before I read your second edit. It seems that you need to do slightly different logic in order to access controls in a content placeholder. This should explain it : msdn.microsoft.com/en-us/library/c8y19k6h.aspx. Of particular importance is that controls in ContentPlaceHolders are not always available when the page is rendered as those areas are intended to be replaced with page content. –  Dave Wise Nov 2 '11 at 14:53
    
Did you see my 3rd edit? I found that the control I need is in a DataFormWebPart. I can get a handle on that web part, but need to access controls within it. –  Tim Gabrhel Nov 2 '11 at 14:58
    
Unfortunately anything within the control will be specific to your exact implementation. The control does provide a controls() collection but I suspect that you will need to use one of the other methods or properties to get the exact control you need : msdn.microsoft.com/en-us/library/… –  Dave Wise Nov 2 '11 at 15:05
    
Thanks a lot for the help. I've accepted your answer as it answers my specific question. However, my issue was occurring because of other things. See my Edit 4 for details. –  Tim Gabrhel Nov 2 '11 at 15:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.