我们如何在 ASP.Net Repeater 中访问循环变量和绑定变量?
How do we access loop variable and bind variable in ASP.Net Repeater?
我在 ASP.Net Web 表单中有一个特殊情况。我有一个中继器,我想在其中循环遍历列表并动态生成控件。
如果我用<%#
,
<% for (var index = 1; index < MyNamespace.Model.Count; index++)
{ %>
<div style="font-weight: bold"><%# Eval(String.Format("v{0}_vendor_name", index.ToString())) %>: </div>
<% } %>
我可以访问 Eval 但不能访问我的循环索引变量。我遇到以下构建错误:
The name 'index' doesn't exist in the current context.
如果我使用<%=
,
<% for (var index = 1; index < MyNamespace.Model.Count; index++)
{ %>
<div style="font-weight: bold"><%= Eval(String.Format("v{0}_vendor_name", index.ToString())) %>: </div>
<% } %>
我没有收到任何构建错误,但在 运行 时,我收到以下错误:
Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control.
我已经研究了这个论坛上的许多问题,但其中 none 要求在一条语句中获取两种类型的变量。
如果您想知道我想要实现什么,我的 DataTable
绑定到这个 Repeater
有一些动态列。我想根据计数动态添加它们。所以,如果有 2 列,我的 Eval 应该选择 v1_vendor_name、v2_vendor_name。
我在DataTable
中有v1_vendor_name
、v2_vendor_name
绑定到这个Repeater
;我没有使用 Eval("v1_vendor_name")
和 Eval("v2_vendor_name")
,而是尝试使用循环,以便动态应用 Eval
中的列。我必须动态地执行此操作,因为我无法控制列数。它可能会上升到 v10_vendor_name
.
我已经尝试使用代码背后的 属性,正如@tweray 所建议的那样,
for (this.index = 1; this.index < MyNamespace.Model.Count; this.index++)
{ %>
<div style="font-weight: bold"><%# Eval(String.Format("v{0}_vendor_name", this.index.ToString())) %>: </div>
<% } %>
但是,即使我在循环中分配了 1,eval 中的 this.index
总是取 0。
我认为你对Eval()
的用法有误解,看这个答案找到what is Eval() use for。
话虽这么说,如果您从代码中删除了 Eval,您就可以开始了:
<% for (var index = 1; index < 3; index++)
{ %>
<div style="font-weight: bold"><%= String.Format("v{0}_vendor_name", index.ToString()) %>: </div>
<% } %>
在Repeater中使用Bind变量循环显然不像MVC。
当 Repeater 中有一个绑定变量时,它希望该字段出现在绑定到该控件的数据集中,而不管 HTML 中存在的条件如何。它不会预先验证条件。
对于这种情况,我将数据集更改为多行而不是动态列,并使用嵌套转发器进行处理。
我在 ASP.Net Web 表单中有一个特殊情况。我有一个中继器,我想在其中循环遍历列表并动态生成控件。
如果我用<%#
,
<% for (var index = 1; index < MyNamespace.Model.Count; index++)
{ %>
<div style="font-weight: bold"><%# Eval(String.Format("v{0}_vendor_name", index.ToString())) %>: </div>
<% } %>
我可以访问 Eval 但不能访问我的循环索引变量。我遇到以下构建错误:
The name 'index' doesn't exist in the current context.
如果我使用<%=
,
<% for (var index = 1; index < MyNamespace.Model.Count; index++)
{ %>
<div style="font-weight: bold"><%= Eval(String.Format("v{0}_vendor_name", index.ToString())) %>: </div>
<% } %>
我没有收到任何构建错误,但在 运行 时,我收到以下错误:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
我已经研究了这个论坛上的许多问题,但其中 none 要求在一条语句中获取两种类型的变量。
如果您想知道我想要实现什么,我的 DataTable
绑定到这个 Repeater
有一些动态列。我想根据计数动态添加它们。所以,如果有 2 列,我的 Eval 应该选择 v1_vendor_name、v2_vendor_name。
我在DataTable
中有v1_vendor_name
、v2_vendor_name
绑定到这个Repeater
;我没有使用 Eval("v1_vendor_name")
和 Eval("v2_vendor_name")
,而是尝试使用循环,以便动态应用 Eval
中的列。我必须动态地执行此操作,因为我无法控制列数。它可能会上升到 v10_vendor_name
.
我已经尝试使用代码背后的 属性,正如@tweray 所建议的那样,
for (this.index = 1; this.index < MyNamespace.Model.Count; this.index++)
{ %>
<div style="font-weight: bold"><%# Eval(String.Format("v{0}_vendor_name", this.index.ToString())) %>: </div>
<% } %>
但是,即使我在循环中分配了 1,eval 中的 this.index
总是取 0。
我认为你对Eval()
的用法有误解,看这个答案找到what is Eval() use for。
话虽这么说,如果您从代码中删除了 Eval,您就可以开始了:
<% for (var index = 1; index < 3; index++)
{ %>
<div style="font-weight: bold"><%= String.Format("v{0}_vendor_name", index.ToString()) %>: </div>
<% } %>
在Repeater中使用Bind变量循环显然不像MVC。
当 Repeater 中有一个绑定变量时,它希望该字段出现在绑定到该控件的数据集中,而不管 HTML 中存在的条件如何。它不会预先验证条件。
对于这种情况,我将数据集更改为多行而不是动态列,并使用嵌套转发器进行处理。