views:

763

answers:

3

In my custom page layout I declare a RichImageField

<PublishingWebControls:RichImageField id="InteriorHeaderImage" FieldName="InteriorHeaderImage" InputFieldLabel="Header Image"  runat="server" DisplayWidth="960" DisplayHeight="242"  />

I'm trying to figure out how to set the default image so the control always shows an image even the first time a page is created using the layout.

Ideally, I would like a declarative approach for example:

<PublishingWebControls:RichImageField id="InteriorHeaderImage" DefaultImageURL="[it should be this easy]" />

It appears that the value property of the control is assigned an ImageFieldValue object which I could probably figure out how to set in a code-behind but this approach seems harder than it should be: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.fields.imagefieldvalue.aspx

Solution: I finally ended up creating a code behind for my page layout by creating a class file that inherits from Microsoft.SharePoint.Publishing.PublishingLayoutPage as described here: http://msdn.microsoft.com/en-us/library/bb986729.aspx

The code of course ended up only being a couple of lines:

protected void Page_Load(object sender, EventArgs e)
        {

            ImageFieldValue imageField = InteriorHeaderImage.Value as ImageFieldValue;
            if (imageField!=null)
            {
                if(string.IsNullOrEmpty(imageField.ImageUrl))
                    imageField.ImageUrl = "/Style Library/assets/images/img1small.jpg";
            }

        }
+1  A: 

I can't test this out at the moment, but the RichImageField field control has a Value property that can be set. Can you try setting this declaratively?

Maybe this will work?:

<PublishingWebControls:RichImageField id="InteriorHeaderImage" Value="[it should be this easy]" />

Alternatively, you could set in the code-behind for the page layout which should be easier than manipulating the ImageFieldValue (as per your link).

dariom
I appreciate your input. I tested your idea, but as expected the value property doesn't expects a string that represents an ImageFieldValue object. I received the following exception:Cannot create an object of type 'System.Object' from its string representation 'test.png' for the 'Value' property.
Craig McKeachie
A: 

The ImageFieldValue inherits from the HtmlTagValue class. The ImageFieldValue has a constructor that accepts the complete <img /> element. Have you tried setting the default value using the complete <img /> element, like <img src="test.png" />?

Waldek Mastykarz - MOSS MVP
Good idea, I tried your idea without any luck unfortunately. I think I'll have to try the code-behind approach.
Craig McKeachie
A: 

Could you create an ImageFieldValue inline?

<PublishingWebControls:RichImageField id="InteriorHeaderImage" runat="server"
    DefaultImageURL='<%# new ImageFieldValue("<img src=\"test.png\" />") %>' />

This would only work if the control's DataBind() method is called at some point.

dahlbyk