0

I'm building several publishing layouts for a public facing SharePoint 2010 website. One of my page layouts is associated with a content type that has about 20 fields.

I'd like to display all these fields in the format of something like:

<p>Field Name</p>
<p>Field Value</p>

Easy enough to do with the FieldLabel and FieldValue controls:

<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
    <SharePointWebControls:FieldLabel id="flSomeField" runat="server" FieldName="SomeField" /> 
    <SharePointWebControls:FieldValue id="fvSomeField" runat="server" FieldName="SomeField" />
</asp:Content>

However, I only want to display the FieldLabel control if the field has a value.

In the code behind of the page layout, I do the following as part of page load:

var listItem = SPContext.Current.ListItem;
if (listItem != null)
{
    var someField = String.Empty;
    if (listItem.TryGetValue<string>("SomeField", out someField))
       if (String.IsNullOrEmpty(someField))
         // Find the control and set its visibility
}

I'm having a hard time finding the FieldLabel control.

I wrote a recursive extension method:

public static T FindControl<T>(this ControlCollection controls, string id) where T : Control
{
    T found = default(T);

    if (controls != null && controls.Count > 0)
    {
        for (int i = 0; i < controls.Count; i++)
        {
            if (controls[i] is T && controls[i].ID.Equals(id, StringComparison.InvariantCultureIgnoreCase))
            {
                found = controls[i] as T;
                break;
            }
            else
                found = FindControl<T>(controls[i].Controls, id);   
        }
    }

    return found;
}

which I call like this:

var flSomeField = this.Controls.FindControl<FieldLabel>("flSomeField");

but it's a recursive mess to say the least...

Any thoughts on how best to do this, or is there another approach I should consider?

3 Answers 3

3

Try adding the member variable:

public class XYX : PublishingLayoutPage {
    protected FieldLabel flSomeField;
}

And ASP.NET should auto-wire it up. Then you can use:

flSomeField.DoStuff();

That is if the naming container is in scope

1
  • Thanks djeeg, I simply needed to add member variables for my controls. Glad the answer was that simple, I was able to go down the custom Field Control route! Thanks Commented May 23, 2011 at 11:09
1

You should be able to access the controls from your code-behind using:

this.flSomeField

Maybe the problem is in the definition of your code-behind class or in the reference to your code file in the Page directive.

Try inserting your OnLoad method inside a script tag in your .aspx file instead of your code-behind file, just to verify that your controls are accessible.

1
  • Thanks Laurie, I added AutoEventWireup="true" and CodeBehind="mylayout.aspx.cs" to my Page directive but it didn't make a difference. I ended up having to do what djeeg suggested, which is to add member variables for my controls to my codebehind class. Commented May 23, 2011 at 11:10
1

You can access it from the sharepoint not from the page itself like this

Microsoft.SharePoint.SPContext.Current.ListItem["FieldName"]

and if you need the field itself not the value

Microsoft.SharePoint.SPContext.Current.ListItem.Fields["FieldName"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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