简体   繁体   English

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

[英]The name ' ' does not exist in the current context

The name 'Label2.Text' does not exist in the current context Catalog.aspx 当前上下文Catalog.aspx中不存在名称“ Label2.Text”

 <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>

Catalog.aspx.cs Catalog.aspx.cs

sqlCon.Open();
SqlCommand cmd_SQL = new SqlCommand("INSERT INTO Cart(ClientId,ProductId,Amount) VALUES (@ClientId,@ProductId,@Amount)", sqlCon);
cmd_SQL.Parameters.Add("@ClientId", SqlDbType.NVarChar).Value =Membership.GetUser().ProviderUserKey.ToString();
cmd_SQL.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value =Label2.Text;
cmd_SQL.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = 1;

cmd_SQL.CommandType = CommandType.Text;
cmd_SQL.ExecuteNonQuery();

The name 'Label2.Text' does not exist in the current context 名称“ Label2.Text”在当前上下文中不存在

Your label is inside of an item template, which means you cannot just access it any old place on the page. 您的标签位于项目模板的内部,这意味着您不能仅在页面上的任何旧位置访问它。 If you want to gain access to the value inside the label, then you need to tie into one of the DataList events, such as OnItemCommand (if you wanted to access the value as the result of a command button click, for instance). 如果要访问标签内的值,则需要绑定DataList事件之一,例如OnItemCommand(例如,如果要通过单击命令按钮来访问该值)。 You can use FindControl inside of the event handler to access the value. 您可以在事件处理程序中使用FindControl访问该值。 For example: 例如:

<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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM