【问题标题】:Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart在 DataFormWebPart 中动态设置 ParameterBinding 的 DefaultValue
【发布时间】:2009-03-04 10:08:05
【问题描述】:

在 WSS 中的自定义 aspx 页面中,我使用带有 xsl 文件的 DataFormWebPart 来呈现一些数据。为了将值传递给 xsl,我使用参数绑定。具体来说,我需要像这样传入服务器主机 url:

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

这很好用,但接下来我要做的是动态获取主机名。因此,为了弄清楚如何从 SharePoint 中获取它,我添加了以下绑定:

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

现在解决问题。如果在页面中的其他位置使用代码,则代码按预期工作,但使用 SharePoint 报告的上述代码:

Web 部件错误:“WebPartPages:DataFormWebPart”的“ParameterBindings”属性 不允许子对象。

有人对此有意见吗?

更新:我已根据this article启用服务器端代码

【问题讨论】:

    标签: asp.net sharepoint dataformwebpart parameterbinding


    【解决方案1】:

    好的,我找到了一个不太优雅但有效的解决方案。

    在尝试了各种操作 ParameterBindings 属性的方法均未成功后,我想到了如何使用 Location 属性获取其中的动态值。

    ParameterBindingLocation 属性指的是从哪里获取值。 this 之类的文章暗示了“Control()”选项。所以将参数绑定改为:

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

    并将以下代码添加到我的页面:

    <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>
    

    ...确实成功了!

    为了从随附的 XSL 文件中获取参数值,我将 param 元素放在根元素中。 param name 属性必须匹配ParameterBinding:

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

    然后可以将参数作为任何其他 XSL 变量进行引用。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 对于 url 的具体情况,我同意,所以 +1。但我的回答也适用于希望传递给 xsl 的任何其他值。
      猜你喜欢
      • 1970-01-01
      • 2016-07-11
      • 2014-02-20
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2011-01-23
      • 1970-01-01
      相关资源
      最近更新 更多