简体   繁体   English

如何在.aspx文件里面访问声明的变量 <asp> 标签元素?

[英]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. 您将无法使用LinkBut​​ton执行此操作。 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.

相关问题 如何在asp.net中将变量值从aspx.cs文件设置为aspx文件? - how to set variable value from aspx.cs file to aspx file in asp.net? 如何在ASPX页面中访问变量? - How to access to variable in ASPX page? 将ASPX CSS文件加载到在母版页中声明的telerik StyleSheetManager中 - Loading a ASPX CSS file into a telerik StyleSheetManager declared inside a master page 如何访问Aspx页面标题内容的javascript标签内的事件对象? - How to access Event object inside javascript tag of Aspx page header content? 如何从aspx访问asp会话 - How access asp session from aspx 如何访问后面代码中的元素,该元素在Xamarin.Foms的xaml页面中的DataTemplate中声明? - How to get access the element in code behind, which is declared inside a DataTemplate in xaml page in Xamarin.Foms? 如何访问 javascript 文件中的 razor 变量? - How to access razor variable inside javascript file? 如何将变量从C#类输出到aspx文件(asp.net) - How can I output variable from C# class to aspx file (asp.net) 如何访问 main 函数中声明的变量值以将其传递给 button_click 方法? C# - How to access variable value declared inside main function to passing it to button_click method ? C# 如何更改在循环内声明的变量? - How to change a variable declared inside a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM