Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart

12.6k views Asked by At

In my custom aspx page in WSS I am using a DataFormWebPart with an XSL file to render some data. In order to pass values to the XSL I use parameter bindings. Specifically, I need to pass in the server host URL like this:

<ParameterBinding 
    Name="HttpHost" 
    Location="CAMLVariable" 
    DefaultValue="http://hardcoded.com" />

This works fine, but the next thing I want to do is to get the host name dynamically. So figuring out how to get that from SharePoint I added the following binding:

<ParameterBinding 
    Name="HttpHost" 
    Location="CAMLVariable" 
    DefaultValue='<%# SPContext.Current.Site.Url.Replace
       (SPContext.Current.Site.ServerRelativeUrl, "") %>' />

Now to the problem. The code works as expected if used some other place in the page, but with the above code SharePoint reports:

Web Part Error: The 'ParameterBindings' property of 'WebPartPages:DataFormWebPart' does not allow child objects.

Anyone have a take on this?

I have enabled server side code according to SharePoint 2007: using ASP.NET server side code in your pages

1

There are 1 answers

0
Peter Lillevold On BEST ANSWER

After trying various methods of manipulating the ParameterBindings property without success I thought of how I could get the dynamic value in there using the Location attribute.

The ParameterBinding Location attribute refers to where to fetch the value from. Articles like this hints of the "Control()" option. So changing the parameter binding to:

<ParameterBinding
  Name="HttpHost"
  Location="Control(MyHttpHost, Text)"
  DefaultValue="" />

and adding the following code to my page:

<asp:TextBox ID="MyHttpHost" runat="server" Visible="false" />
<script runat="server">
protected void Page_Load()
{
  MyHttpHost.Text = 
   SPContext.Current.Site.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, ""); 
}
</script>

...actually did the trick!

To get to the parameter values from within the accompanying XSL file I put param elements in the root element. The param name attribute must match that of the ParameterBinding:

<xsl:stylesheet ...>
    ...
    <xsl:param name="HttpHost"/>

The parameter can then be referenced as any other XSL variable.