如何将数据绑定表达式的值存储到变量中

时间:2021-12-30 03:48:20

I need to access the value of a bound item several times in a template. Right now my ListView template looks like this:

我需要在模板中多次访问绑定项的值。现在我的ListView模板如下所示:

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="plc"><br/>
 <ItemTemplate><br/>
  <input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="3" /><br/>
  <input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="4" /><br/>
    </ItemTemplate><br/>
    <LayoutTemplate><br/>
        <div id="plc" runat="server"><br/>
        </div><br/>
    </LayoutTemplate><br/>
    <EmptyDataTemplate><br/>
        No data</EmptyDataTemplate><br/>
</asp:ListView><br/>

Under certain conditions I may have dozens of radio button so repeatedly calling <%# DataBinder.Eval(Container.DataItem, "Day")%> seems to be inefficient.

在某些情况下,我可能会有几十个单选按钮,因此反复调用<%#DataBinder.Eval(Container.DataItem,“Day”)%>似乎效率低下。

I would like to assign the value of that expression to a variable and then use this variable instead so my template would look something like this

我想将该表达式的值赋给变量,然后使用此变量,因此我的模板看起来像这样

<ItemTemplate><br />
<%String ClassName = "myrating" + <%# DataBinder.Eval(Container.DataItem, "Day")%><br />
  <input type="radio" class="<%=ClassName %>" value="3" /><br />
  <input type="radio" class="<%="ClassName" value="4" /><br />
    </ItemTemplate><br />

This example doesn't compile but I hope you are getting the idea.

这个例子没有编译,但我希望你能得到这个想法。

4 个解决方案

#1


You can use OnItemDataBount event and work with DataItem as with variable there.

您可以使用OnItemDataBount事件并使用DataItem作为变量。

#2


You can give your page a public variable MyRating.

您可以为页面提供一个公共变量MyRating。

Now you can assign the variable in the expression binding Syntax:

现在您可以在表达式绑定语法中分配变量:

   <ItemTemplate>
         <%# MyRating = "myrating" + <%# Eval(Container.DataItem, "Day")%>
         //Use the variable inside the binding(!) block 
         <%#MyRating
   </ItemTemplate>

I usually bind to lists of view-objects. That way I can access view properties directly.

我通常绑定到视图对象列表。这样我就可以直接访问视图属性。

   <ItemTemplate>
         <%# MyType = (MyType)Container.DataItem 
         <%# MyRating.Average %> 
         <%# MyRating.Count %> 
   </ItemTemplate>

Hope this helps :-)

希望这可以帮助 :-)

#3


I personally consider setting HTML element values in the OnItemDataBound event to be messier than in the ASP code. I also didn't like setting the variable value using a data-binding expression since it seems to cause the value to be output in the HTML.

我个人认为在OnItemDataBound事件中设置HTML元素值比在ASP代码中更加混乱。我也不喜欢使用数据绑定表达式设置变量值,因为它似乎导致值在HTML中输出。

Here's a similar way to do it based on the other answers:

基于其他答案,这是一种类似的方法:

1. Create a protected field in the code-behind to use as your variable.

protected string className;

2. Use a data-binding expression to assign to the variable.

<asp:Literal runat="server" Visible="false" Text="<%# className = "myrating" + DataBinder.Eval(Container.DataItem, "Day") %>" />

Make sure to do this inside the hidden server-side tag so that the result of the expression does not appear in the resulting HTML.

确保在隐藏的服务器端标记内执行此操作,以便表达式的结果不会出现在生成的HTML中。

3. Use the variable inside data-binding expressions in the ASP code.

<ItemTemplate><br />
    <input type="radio" class="<%# className %>" value="3" /><br />
    <input type="radio" class="<%# className %>" value="4" /><br />
</ItemTemplate><br />

Make sure to use data-binding expressions to access the variable. Other expression types only seem to see the default value of the variable.

确保使用数据绑定表达式来访问变量。其他表达式类型似乎只能看到变量的默认值。

I think the main disadvantage of this approach is the use of the field, which would ideally be scoped to the ItemTemplate element.

我认为这种方法的主要缺点是使用了字段,理想情况下,该字段应限定为ItemTemplate元素。

#4


You can define public/protected variables in the code-behind and assign those in data binding expressions.

您可以在代码隐藏中定义公共/受保护变量,并在数据绑定表达式中分配它们。

If you don't have access to the code-behind (e.g. when you are modifying the markup of a compiled application), you can declare variables in a script block set to run on server, e.g.:

如果您无法访问代码隐藏(例如,当您修改已编译应用程序的标记时),则可以在脚本块集中声明变量以在服务器上运行,例如:

<script runat="server">
    YourNamespace.Rating current;
</script>

Then in your control's binding templates:

然后在你的控件的绑定模板中:

<HeaderTemplate>
    <%# (current = (YourNamespace.Rating)Eval("Day")) == null ? "" : "" %>
</HeaderTemplate>

Put it in the HeaderTemplate if you only want it to get executed once per databinding.
The == null ? "" : ""-part, is to prevent any generated cruft html (e.g. the ToString value of the assigned value), from being rendered in the browser.

如果您只希望每个数据绑定执行一次,请将它放在HeaderTemplate中。 == null? “”:“ - ”部分是为了防止在浏览器中呈现任何生成的残缺html(例如指定值的ToString值)。

#1


You can use OnItemDataBount event and work with DataItem as with variable there.

您可以使用OnItemDataBount事件并使用DataItem作为变量。

#2


You can give your page a public variable MyRating.

您可以为页面提供一个公共变量MyRating。

Now you can assign the variable in the expression binding Syntax:

现在您可以在表达式绑定语法中分配变量:

   <ItemTemplate>
         <%# MyRating = "myrating" + <%# Eval(Container.DataItem, "Day")%>
         //Use the variable inside the binding(!) block 
         <%#MyRating
   </ItemTemplate>

I usually bind to lists of view-objects. That way I can access view properties directly.

我通常绑定到视图对象列表。这样我就可以直接访问视图属性。

   <ItemTemplate>
         <%# MyType = (MyType)Container.DataItem 
         <%# MyRating.Average %> 
         <%# MyRating.Count %> 
   </ItemTemplate>

Hope this helps :-)

希望这可以帮助 :-)

#3


I personally consider setting HTML element values in the OnItemDataBound event to be messier than in the ASP code. I also didn't like setting the variable value using a data-binding expression since it seems to cause the value to be output in the HTML.

我个人认为在OnItemDataBound事件中设置HTML元素值比在ASP代码中更加混乱。我也不喜欢使用数据绑定表达式设置变量值,因为它似乎导致值在HTML中输出。

Here's a similar way to do it based on the other answers:

基于其他答案,这是一种类似的方法:

1. Create a protected field in the code-behind to use as your variable.

protected string className;

2. Use a data-binding expression to assign to the variable.

<asp:Literal runat="server" Visible="false" Text="<%# className = "myrating" + DataBinder.Eval(Container.DataItem, "Day") %>" />

Make sure to do this inside the hidden server-side tag so that the result of the expression does not appear in the resulting HTML.

确保在隐藏的服务器端标记内执行此操作,以便表达式的结果不会出现在生成的HTML中。

3. Use the variable inside data-binding expressions in the ASP code.

<ItemTemplate><br />
    <input type="radio" class="<%# className %>" value="3" /><br />
    <input type="radio" class="<%# className %>" value="4" /><br />
</ItemTemplate><br />

Make sure to use data-binding expressions to access the variable. Other expression types only seem to see the default value of the variable.

确保使用数据绑定表达式来访问变量。其他表达式类型似乎只能看到变量的默认值。

I think the main disadvantage of this approach is the use of the field, which would ideally be scoped to the ItemTemplate element.

我认为这种方法的主要缺点是使用了字段,理想情况下,该字段应限定为ItemTemplate元素。

#4


You can define public/protected variables in the code-behind and assign those in data binding expressions.

您可以在代码隐藏中定义公共/受保护变量,并在数据绑定表达式中分配它们。

If you don't have access to the code-behind (e.g. when you are modifying the markup of a compiled application), you can declare variables in a script block set to run on server, e.g.:

如果您无法访问代码隐藏(例如,当您修改已编译应用程序的标记时),则可以在脚本块集中声明变量以在服务器上运行,例如:

<script runat="server">
    YourNamespace.Rating current;
</script>

Then in your control's binding templates:

然后在你的控件的绑定模板中:

<HeaderTemplate>
    <%# (current = (YourNamespace.Rating)Eval("Day")) == null ? "" : "" %>
</HeaderTemplate>

Put it in the HeaderTemplate if you only want it to get executed once per databinding.
The == null ? "" : ""-part, is to prevent any generated cruft html (e.g. the ToString value of the assigned value), from being rendered in the browser.

如果您只希望每个数据绑定执行一次,请将它放在HeaderTemplate中。 == null? “”:“ - ”部分是为了防止在浏览器中呈现任何生成的残缺html(例如指定值的ToString值)。