문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top