[英]How To Access Declared Variable in .aspx File Inside <asp> Tag Element?
I've have code below : 我有以下代码:
<div class="row">
<%
foreach (Product product in products)
{
%>
<div class="col-md-3">
<asp:LinkButton
class="card df gdc aic"
OnClick="btnDetail_Click"
ID="btnDetail"
runat="server">
<%= product.Name %>
</asp:LinkButton>
</div>
<%
}
%>
The code <%= product.Name %>
produce an error that say, The name product
does not exist int the current context. 代码
<%= product.Name %>
产生一个错误,即product
名称product
在当前上下文中不存在。
How to access that product
variable ? 如何访问该
product
变量?
You won't be able to do that using a LinkButton. 您将无法使用LinkButton执行此操作。 It would work with a normal HTML link though.
它可以使用普通的HTML链接。 You should use a Repeater instead:
您应该使用Repeater:
<asp:Repeater runat="server" ID="productsList">
<ItemTemplate>
<div class="col-md-3">
<asp:LinkButton
class="card df gdc aic"
ID="btnDetail"
runat="server"
Text='<%# Eval("Name") %>'>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
productsList.DataSource = products;
productsList.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.