在ASP中找不到GridView

时间:2011-03-26 23:07:28

标签: asp.net shopping-cart

我是asp的新手,并下载了购物车的一些示例代码。当我在Visual Studio中创建一个新项目时,添加了所有文件并尝试编译我得到了以下错误。

  

错误1当前上下文中不存在名称'gvShoppingCart'c:\ users \ slaphappysmoker \ documents \ visual studio 2010 \ Projects \ Shopcart \ Shopcart \ ViewCart.aspx.cs 21 3 Shopcart

但是我可以看到在ViewCart.aspx文件中定义gvShoppingCart的位置。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewCart.aspx.cs" Inherits="ViewCart" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Shopping Cart</title>
    <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h1>Shopping Cart</h1>
            <a href="Default.aspx">&lt; Back to Products</a>
            <br /><br />
            <asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
                <HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />
                <FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />
                <AlternatingRowStyle BackColor="#F8F8F8" />
                <Columns>
                    <asp:BoundField DataField="Description" HeaderText="Description" />
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
                            <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>' style="font-size:12px;"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
                    <asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" />
        </div>
    </form>
</body>
</html>

以下是在ViewCart.aspx.cs文件中调用GridView的代码:

protected void BindData() {
    // Let's give the data to the GridView and let it work!
    // The GridView will take our cart items one by one and use the properties
    // that we declared as column names (DataFields)
    gvShoppingCart.DataSource = ShoppingCart.Instance.Items;
gvShoppingCart.DataBind();
}

如何让ViewCart.aspx.cs代码文件识别ViewCart.aspx和GridView中包含的GridView?

编辑:添加了调用gridview的函数。

1 个答案:

答案 0 :(得分:1)

尝试确保gridview在designer.cs文件中有引用

protected global :: System.Web.UI.WebControls.GridView gvShoppingCart;