将页面上的方法中的链接按钮链接到母版页中的链接按钮

时间:2017-05-01 13:46:17

标签: c# asp.net web

我正在尝试使用ASP.NET为我的基于C#的网站制作购物车系统。

目前我已经创建了一个方法来更新我在母版页中的链接按钮。

当我尝试在产品页面中引用链接按钮时,错误地说'productpages_stand'不包含'lbnCart'的定义,并且没有扩展方法'lbnCart'可以找到'productpages_stand'类型的第一个参数'。

我无法将链接按钮从网络表单链接到母版页吗?

方法:

private void updateCartSummary()
{
    // get number of items in cart and show summary in link button
    ArrayList cart = (ArrayList)Session["CART"];
    int totalItems = cart.Count;
    this.lbnCart.Text = "Cart : " + "(" + totalItems + ")";
}

主页的链接按钮:

<asp:LinkButton ID="lbnCart" CssClass="shoppingcarttext button small third" runat="server">Cart : (0)</asp:LinkButton>

编辑: 产品页面上出错的主要代码段:

public partial class productpages_atomosninja : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) // first time
        {
            updateCartSummary();

        }

        LinkButton lbnCart = (LinkButton)Master.FindControl("lbnCart");
        if (lbnCart != null)
        {
            lbnCart.Text = "Cart : " + "(" + totalItems + ")";
        }

    }

    protected void btnAtomos_Click(object sender, EventArgs e)
    {
        Trace.Warn("Adding an item to the cart");
        // create cart item object with the book details
        CartItem cartItem = new CartItem();
        cartItem.setCost(299.00);
        cartItem.setItemName("Atomos Ninja 2");

        // extract arraylist from session variable
        ArrayList arrCart = (ArrayList)Session["CART"];

        // add the cartitem object to the arraylist
        arrCart.Add(cartItem);

        //store arrayList back into the session variable
        Session.Add("CART", arrCart);

        updateCartSummary();

    }

    private void updateCartSummary()
    {
        // get number of items in cart and show summary in link button
        ArrayList cart = (ArrayList)Session["CART"];
        int totalItems = cart.Count;
        this.lbnCart.Text = "Cart : " + "(" + totalItems + ")";
    }

}

page_load方法中totalItems的错误,说它不存在,因为它直到文档的后面才定义。

编辑2: 添加代码后@David表示仍然存在错误。

public partial class product : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack) // first time
        {
            updateCartSummary();

        }

    }

    LinkButton lbnCart = (LinkButton)Master.FindControl("lbnCart");
        if (lbnCart != null)
        {
            lbnCart.Text = "Cart : " + "(" + totalItems + ")";
        }

}

1 个答案:

答案 0 :(得分:0)

虽然母版页和内容页之间存在关系,但这并不会改变C#语言中类和属性以及一般范围的工作方式。母版页和内容页是不同的类,因此您无法直接引用彼此的类成员。

但是,内容页的类在其上有一个名为Master的属性,它引用了母版页。该属性可用于http://freelancing-gods.com/thinking-sphinx/searching.html#filters

例如:

private void updateCartSummary()
{
    // this part is unchanged from what you have
    ArrayList cart = (ArrayList)Session["CART"];
    int totalItems = cart.Count;

    // here, find the control from the "Master" property
    LinkButton lbnCart = (LinkButton) Master.FindControl("lbnCart");
    if(lbnCart != null)
    {
        lbnCart.Text = "Cart : " + "(" + totalItems + ")";
    }
}
相关问题