Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称'';在当前上下文中不存在_C#_Sql_Asp.net - Fatal编程技术网

C# 名称'';在当前上下文中不存在

C# 名称'';在当前上下文中不存在,c#,sql,asp.net,C#,Sql,Asp.net,当前上下文中不存在名称“Label2.Text” Catalog.aspx <asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatLayout="Flow"> <ItemTemplate> <div class="Item"> <

当前上下文中不存在名称“Label2.Text” Catalog.aspx

 <asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatLayout="Flow">
    <ItemTemplate>
        <div class="Item">
            <div class="name">
                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </div>
            <div>
                Код:<asp:Label ID="Label2" runat="server" Text='<%# Eval("Id") %>' />
            </div>
            <img src="<%# Eval("Image") %>" height="115" alt="item"/>
            <div class="price">
                Цена:

                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price")%>' />p.
                <asp:Button ID="Button2" runat="server" ForeColor="Black" 
                    onclick="Button2_Click" Text="В КОРЗИНУ" />
            </div>
            <div class="desc">
                <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />

            </div>
        </div>
    </ItemTemplate>
</asp:DataList>

当前上下文中不存在名称“Label2.Text”

您的标签位于项目模板内,这意味着您不能仅访问页面上的任何旧位置。如果希望访问标签内的值,则需要绑定到一个DataList事件,例如OnItemCommand(例如,如果希望通过命令按钮单击来访问值)。您可以在事件处理程序内部使用FindControl来访问该值。例如:

<asp:DataList runat="server" ID="test" OnItemCommand="test_ItemCommand">
    <ItemTemplate>
        <asp:Label runat="server" ID="Label2" Text="Test" />
    </ItemTemplate>
</asp:DataList>

这个.aspx文件中的头是什么样子的。您是否使用
CodeFile=
codebeahind=
如果确保您有
codebeahind=
@M.kazem-Akhgary,您的评论有何帮助或效果。很明显,后面的代码找不到
Label2.Text
您的实际评论在aspx文件上显示
页面
声明。
<asp:DataList runat="server" ID="test" OnItemCommand="test_ItemCommand">
    <ItemTemplate>
        <asp:Label runat="server" ID="Label2" Text="Test" />
    </ItemTemplate>
</asp:DataList>
protected void test_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
    if (e.Item != null)
    {
        var label2 = e.Item.FindControl("Label2");

        if (label2 != null && label2 is Label)
        {
            var productID = ((Label)label2).Text;

            // now you have the contents of the label's text property
        }
    }
}