Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart

Peter Lillevold picture Peter Lillevold · Mar 4, 2009 · Viewed 12.5k times · Source

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?

Update: I have enabled server side code according to this article

Answer

Peter Lillevold picture Peter Lillevold · Mar 5, 2009

Ok, I found a solution that is not that elegant but it works.

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.