设置FieldRenderer的Item或DataSource属性的好方法是什么? [英] What's a good way to set the Item or DataSource attribute of a FieldRenderer?

查看:176
本文介绍了设置FieldRenderer的Item或DataSource属性的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种情况是我有很多FieldRenderer.这些应该从各个位置输出数据,一些来自X项,其他来自Y项.应该从Z项输出属性.

The scenario is that I have a lot of FieldRenderers. These should output data from various places, some from item X and others from item Y. And should be outputting properties from item Z.

假设我有一个公共属性 ItemX ,我想从中输出属性,那么以下任何一项都可以.但是我没有得到任何输出:

Assuming I have a public property ItemX that I want to output a property from, any of the following would be OK. But I get no output by any of them:

<sc:FieldRenderer runat="server" FieldName="Logo" DataSource="<%# ItemX %>" />
<sc:FieldRenderer runat="server" FieldName="Logo" DataSource="<%= ItemX.Paths.FullPath %>" />
<sc:FieldRenderer runat="server" FieldName="Logo" Item="<%# ItemX %>" />
<sc:FieldRenderer runat="server" FieldName="Logo" Item-ID="<%# ItemX.ID %>" />
<sc:FieldRenderer runat="server" FieldName="Logo" Item-ID-Guid="<%# ItemX.ID.Guid %>" />

如果我向其中添加一个ID MyFieldRenderer 并执行以下丑陋的操作,则会得到正确的输出:

If I add an ID MyFieldRenderer to it and do the ugly piece below, I get correct output:

MyFieldRenderer.Item = ItemX;

必须有更好的方法来做到这一点?我不确定这是特定于Sitecore还是WebForms问题.

There must be a better way to do this? I'm not sure if this is a Sitecore-specific or WebForms question.

推荐答案

在不使用背后代码的情况下,无法将FieldRenderer的数据源设置为服务器端对象.这是WebForms如何工作的结果. Microsoft知识库文章:

It is not possible to set the datasource of a FieldRenderer to a server side object without using code behind. This is a consequence of how WebForms works. The issue is described in a Microsoft Knowledge Base article:

<%= ...%>显示表达式等效于仅包含Response.Write(…)语句的嵌入式代码块.这是显示信息的最简单方法,例如单个字符串,一个int变量或一个常数.[...] 请记住,显示表达式不能在服务器控件的属性中使用.这是因为.NET Framework直接编译整个表达式,而不是将内容显示为属性的值.

换句话说,.NET希望编译sc:FieldRenderer,因此无法访问<%= ItemX.Paths.FullPath%>的运行时内容.通过尝试显示,您可以以更简单的形式看到此问题:

In other words, .NET wants to compile the sc:FieldRenderer, and so does not have access to the run-time contents of <%= ItemX.Paths.FullPath %>. You can see this issue in simpler form by trying to display:

<asp:TextBox runat="server" Text="<%= DateTime.Now.ToString() %>" />

会在文本框中显示<%= DateTime.Now.ToString()%> .简而言之,除了服务器控件属性中的静态字符串之外,您什么都不能得到.

which renders <%= DateTime.Now.ToString() %> inside the text box. In short, you cannot get anything other than a static string inside a server control attribute.

有几种解决此问题的方法:

There are several possible solutions to this issue:

  1. 在您的Page_Load方法中,按照您的描述设置FieldRenderer的项目"字段.如果需要使用此逻辑的子布局的数量受到限制,这是最好的方法.

  1. In your Page_Load method, set the Item field of the FieldRenderer, as you describe. This is the best approach if the number of sublayouts that need to use this logic is limited.

您可以创建将Item绑定到ItemX的ItemXFieldRenderer子类:

You can create an ItemXFieldRenderer subclass that binds Item to ItemX:

 class ItemXFieldRenderer: FieldRenderer {
   public ItemXFieldRenderer() {
       Item = [code to retrieve ItemX];
   }
 }

然后,您可以在解决方案中要从ItemX渲染字段的任何地方使用此控件.如果大量子布局需要使用此逻辑,并且您可能需要绑定的项目数量非常有限,则这是最佳方法.

Then you can use this control anywhere in your solution where you want to render a field from ItemX. This is the best approach if a large number of sublayouts need to use this logic and the number of items you might need to bind to is very limited.

您可以创建FieldRenderer的子类,该子类分析字符串属性并使用逻辑将字符串值映射到正确的项目.

You could create a subclass of FieldRenderer that parses a string property and uses logic to map the string value to the correct item.

如果ItemX的路径恒定,则可以在Datasource属性中设置完整路径,如下所示:

If the path to ItemX is constant, you can set the full path in the Datasource property like this:

<sc:FieldRenderer runat="server" FieldName="Logo"
DataSource="/sitecore/context/home/some/item" />

您也可以使用相对路径.例如,如果上下文项具有一个名为"Sources"的子文件夹,而该子文件夹又具有一个"Default"子项,则可以使用以下语法在FieldRenderer中进行引用:

You can also use a relative path. For example, if the context item has a child folder called "Sources", which in turn has a child item "Default", you could reference in your FieldRenderer with this syntax:

<sc:FieldRenderer runat="server" FieldName="Logo"
DataSource="sources/default" />

根据我的测试,对数据源的评估不区分大小写,并且Sitecore Query表达式(例如"../.."和"//* [@@ name ='value']")不起作用.

Per my testing, the evaluation of the datasource is case-insensitive, and Sitecore Query expressions like "../.." and "//*[@@name='value']" do not work.

您可以按照本论坛线程:

<sc:FieldRenderer id="myRenderer" FieldName="Logo" 
DataSource=<%# ItemX.Paths.FullPath %> />

然后在代码背后,添加

myRenderer.DataBind(); 

使用最后一种方法,您仍在使用代码隐藏功能,但是标记中现在包含决定哪个FieldRenderer使用哪个项目的决定.正如Christian Hagelid 指出所指出的那样,您可以在子布局上调用this.DataBind()来强制DataBind在所有子对象上递归执行页面上的控件.

With this last approach you are still using codebehind, but the decision of which FieldRenderer uses which item is now contained in the markup. And as Christian Hagelid points out, you can call this.DataBind() on the sublayout to force DataBind to execute recursively on all controls on the page.

您可以使用ASP.NET的ExpressionBuilder语法来集中数据源路径的位置.有三种方法可以做到这一点:

You can use ASP.NET's ExpressionBuilder syntax to centralize the location of your datasource paths. There are three ways of doing this:

  • 将路径放置在Web.config中.将此添加到< AppSettings>Web.config部分:

  • Places your paths in Web.config. Add this to the <AppSettings> section of Web.config:

<添加key ="ItemX" value ="/sitecore/content/path/to/itemx"/>
然后将DataSource属性设置为:

<add key="ItemX" value="/sitecore/content/path/to/itemx" />
Then set the DataSource attribute to:

DataSource =<%$ AppConfig:ItemX%>

将路径放置在App_GlobalSettings中的.resx资源文件中.如果文件名为Paths.resx,则可以使用以下语法访问其设置:

Place the paths in a .resx resource file in App_GlobalSettings. If the file is named Paths.resx, you can access its settings with this syntax:

DataSource =<%$资源:Paths,ItemX%>

您可以构建 ExpressionBuilder 类以构建将字符串值转换为路径的逻辑.请注意,ExpressionBuilder在解析时进行评估,因此您将无权访问Sitecore上下文.这看起来并不简单:表达式构建器需要在Web.config中引用,而代码则需要驻留在App_Code中.

You can build an ExpressionBuilder class to build logic to translate a string value to a path. Note that the ExpressionBuilder is evaluated at Parse time, so you will not have access to the Sitecore context. This does not look straightforward: the expression builder needs to be referenced in Web.config and the code needs to reside in App_Code.

这篇关于设置FieldRenderer的Item或DataSource属性的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆