-1

I'm attempting to make a cart system for my C# based website using ASP.NET.

Currently I've created a method which updates a linkbutton which I have in the master page.

When I try and reference the linkbutton in a product page it errors saying 'productpages_stand' does not contain a definition for 'lbnCart' and no extension method 'lbnCart' accepting a first argument of type 'productpages_stand' could be found'.

Am I not able to link a linkbutton to a masterpage from a web form?

The method:

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 + ")";
}

Link button from masterpage:

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

EDIT: Main code segment which is erroring on the product page:

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 + ")";
    }

}

Error for totalItems in the page_load method saying it doesn't exist since it isn't defined until later in the document.

EDIT 2: After adding the code where @David said it still errors.

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 + ")";
        }

}
killerwild
  • 117
  • 1
  • 2
  • 9

1 Answers1

0

While there is a relationship between the Master Page and the Content Page, this doesn't change how classes and properties and general scope work in the C# language. The Master Page and the Content Page are different classes, so you can't directly reference each other's class members like that.

However, the class for the Content Page does have a property on it called Master which references the Master Page. And that property can be used to access members of the Master Page object.

For example:

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 + ")";
    }
}
David
  • 208,112
  • 36
  • 198
  • 279
  • I tried doing it but it said that the 'totalItems' name doesn't exist in the current context. The reason for this is as it's in the updateCartSummary. I tried placing the updateCartSummary method above the Page_Load method but still doesn't pick up 'totalItems'. – killerwild May 01 '17 at 14:17
  • @killerwild: What did you change to make that happen? In your question you declare the `totalItems` variable just before trying to use it to set the value on the Master Page. Did you move that declaration somewhere else? Update the question to show the current code. – David May 01 '17 at 14:19
  • The updateCartSummary method is to update the linkbutton which shows in a nav bar at the top which is created in the product master page. I copied your example and tried, then tried adding all the code in which worked but the lbnCart on the actual product page errored still with both attempts. The example I'm following has a single page to add products to the basket but on my site I have a separate page for each product. – killerwild May 01 '17 at 14:22
  • @killerwild: Unless you show the code which causes the problem, there isn't much anybody here can do to help. – David May 01 '17 at 14:24
  • Added the code which is erroring. Hopefully it's more helpful. – killerwild May 01 '17 at 14:27
  • @killerwild: You haven't updated your `updateCartSummary()` code *at all*. You're getting the same error because you're still trying to do the same thing. `lbnCart` doesn't exist in the `productpages_atomosninja` class. To access controls in the related Master Page, you have to find those controls in the `Master` property as demonstrated in this answer. – David May 01 '17 at 14:30
  • I tried that but as I said, when I put it in the page_load method of the content page it says 'The name 'totalItems' does not exist in the current context'. – killerwild May 01 '17 at 14:46
  • @killerwild: The code you posted doesn't have any reference to `totalItems` in the `Page_Load()` method, so the error you're describing makes no sense. In any event, `totalItems` is in your `updateCartSummary()` method. In the code you posted, that's the only method which uses that value and no error related to that value exists. – David May 01 '17 at 14:49
  • I've added the updated code with the code you suggested in the page_load and errors as I said. – killerwild May 01 '17 at 15:03
  • @killerwild: Why did you put that code in `Page_Load()`? You're trying to update the control on the Master Page in the `updateCartSummary()` method. So that's where you would apply the corrected code to update the control on the Master Page. – David May 01 '17 at 15:04
  • I put the code in the master page, in the page_load as the link you linked suggested and also not in the page_load method and neither worked. When outside it errored lbnCart, Master and totalItems. Added code to question to show. – killerwild May 01 '17 at 15:42
  • @killerwild: Nobody has suggested that you change anything in `Page_Load()`. In your *original question* you had a specific error happening on a single line of code. This answer suggests that you replace that single non-working line of code with a couple lines of working code. Whatever random changes you've also been making entirely outside the scope of what you originally asked... I can't imagine what those changes are or why you're making them. – David May 01 '17 at 15:44
  • Okay, so I added that code but the problem is still not resolved. Now it's saying 'System.NullReferenceException: Object reference not set to an instance of an object.' for 'int totalItems = cart.Count;' in the contents page. – killerwild May 01 '17 at 16:38
  • @killerwild: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – David May 01 '17 at 16:39