Results 1 to 16 of 16

Thread: [RESOLVED] Refreshing Web User Control

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Resolved [RESOLVED] Refreshing Web User Control

    I have a web user control named shopcart which is nested in the master page.

    Then i have catalog CONTENT PAGE which is using the same master page.


    When i add an item to the shopping cart it does not reflect the small shoppingcart immediately.


    Rather, when i go to another page of click the refresh button the newly added item gets displayed in the shopcart.


    The web user control has a method PopulateShopCart in the Load Event Handler but it is not fired when i add new item from the catalog.


    How do i make it refreshing immediatelly if i add a new item in the shopcart.


    Thanks
    Last edited by selanec; Jan 2nd, 2010 at 11:06 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Refreshing Web User Control

    You haven't told us how the shopping cart has been implemented. It's likely though, that you're not seeing results because the add-to-cart code is being executed after the user control refreshes.

    To handle this is going to be a little complicated if you haven't done this before (but once you do it it's easy). Your content page will then explicitly call a method in the Master Page. Your Master Page will then call a method in the cart user control which you need to expose.

    So to do this, start by going to your shopping cart ASCX and make that method public (the method that adds an item).

    Next, add a public method to the Master Page and in this method, call the above public method (obviously, they need to have the same parameters). This is simple enough

    Code:
    public void AddShoppingCartItem(string itemName, int itemId)
    {
       shoppingCart1.AddItem(itemName, itemId);
    }
    Finally, in your content page, you need to call the Master Page's public method. The code for that is something like this (experiment). Assuming your MasterPage is called MySiteMaster.master

    Code:
    MySiteMaster master = this.Master as MySiteMaster;
    if(master != null)
    {
       master.AddShoppingCartItem("Xbox", 23489324);
    }

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Refreshing Web User Control

    selanec,

    If you haven't done so already, you might want to take a look at the following:

    http://www.dotshoppingcart.com/

    Which was found from here:

    http://www.asp.net/community/projects/

    Gary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Refreshing Web User Control

    Quote Originally Posted by mendhak View Post
    You haven't told us how the shopping cart has been implemented. It's likely though, that you're not seeing results because the add-to-cart code is being executed after the user control refreshes.

    To handle this is going to be a little complicated if you haven't done this before (but once you do it it's easy). Your content page will then explicitly call a method in the Master Page. Your Master Page will then call a method in the cart user control which you need to expose.

    So to do this, start by going to your shopping cart ASCX and make that method public (the method that adds an item).

    Next, add a public method to the Master Page and in this method, call the above public method (obviously, they need to have the same parameters). This is simple enough

    Code:
    public void AddShoppingCartItem(string itemName, int itemId)
    {
       shoppingCart1.AddItem(itemName, itemId);
    }
    Finally, in your content page, you need to call the Master Page's public method. The code for that is something like this (experiment). Assuming your MasterPage is called MySiteMaster.master

    Code:
    MySiteMaster master = this.Master as MySiteMaster;
    if(master != null)
    {
       master.AddShoppingCartItem("Xbox", 23489324);
    }
    The code AddShopcartItem is method of the class ShopcartAccess which is located in the App_Code ASP.NET folder. And it works just fine! The only flaw is that the small shopping cart does not refresh immediately althrough the item is already added and can be found in the shoppingcart table in the database (MSSQL). Can i resolve this issue without changing the whole concept? Thanks

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Refreshing Web User Control

    Regardless of how you do it, the ShopCartAccess.AddShopCartItem() method is being called after the shopping ASCX has processed/loaded, so anything that happens after its loading is useless to it, unless you explicitly get it to reload, which you do by introducing a public method that will get it to read from the database again or rebind.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Refreshing Web User Control

    Quote Originally Posted by mendhak View Post
    Regardless of how you do it, the ShopCartAccess.AddShopCartItem() method is being called after the shopping ASCX has processed/loaded, so anything that happens after its loading is useless to it, unless you explicitly get it to reload, which you do by introducing a public method that will get it to read from the database again or rebind.
    Exactly but, how do i call this method which refers some webusercontrol's controls e.g.

    Code:
    Public Sub PopulateShopCart() 
      If ShopcartAccess.GetItems.Rows.Count > 0 Then
         RepeaterShopcart.DataSource = ShopcartAccess.GetItems
         RepeaterShopcart.DataBind()
         labelGrandTotal = ShopcartAccess.GetTotalAmount()
    
      Else
         RepeaterShopcart.DataSource = Nothing
         RepeaterShopcart.DataBind()
         labelGrandTotal = "0.00"
      End If
    End Sub
    I just posted short version of the PopulateSHopCart sub which is much longer and which refers more controls on the web user controls itself.

    SHould i change the scope of this method so i can access it from the content page or what?

    Thanks a lot and Happy New Year

  7. #7
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Refreshing Web User Control

    If you follow what mendhak first posted it goes like ... the page is a child of the master (sort of) the userControl is a child of the master so set a public method on the master that calls a public method on the userControl. call the public method on the master from the page AFTER the shopping cart has been updated that way the data is current.

    It might seem painful but it is logical if you follow the chain of events raised each postback for each page/control. I often use puplic properties like p_load that I can call at any point in the event cycle.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Refreshing Web User Control

    Ok i see the point, but i am not sure how to do that. Actually i was hoping that i can still keep the existing approach with slight modification(s).
    In addition, what do you recommend? To make a brand new Method in the Master to refer the WebUserControl's controls or just call this method somehow. Sorry but i have never been in such situation before.
    Thanks

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Refreshing Web User Control

    PopulateShopCart is in the Master, yes? That's half your work done. Call PopulateShopCart from your content page.

    vb Code:
    1. Dim myMaster As MySiteMaster = CType(Page.Master, MySiteMaster)
    2. myMaster.PopulateShopCart()

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Refreshing Web User Control

    Actually NO.
    The method PopulateShopCart is in the WEB USER CONTROL (.ascx) which is loaded in the Master.master page.

    Code:
    <&#37;@ Master Language="VB" CodeFile="Master.master.vb" Inherits="MasterPage" %>
    <%@ Register Src="~/ctrls/shopcart.ascx" TagName="shopcart" TagPrefix="ctrl" %>
    
    <!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 runat="server" visible="false">Title</title>
        <asp:Literal ID="LiteralHeaderContent" runat="server"></asp:Literal>
        <asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
    
    blah blah ...
    
    <asp:ContentPlaceHolder id="TheContent" runat="server" /> 
    
    blah blah ....
    
    <ctrl:shopcart id="CartControl" runat="server" />
    
    blah blah ...
    
    </form>
    </body>
    </html>
    the ascx code behind contains this Sub which is called on every load execution:

    Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            PopulateShopCart()
        End Sub
    Mayby i should use Find Control referring the master page and then call the user control's method?

    Thanks

    EDIT:

    I tried this, but it complains that PopulateShopCart is not a member of System.Web.UI.Control

    Code:
     Dim myctrl As Control = Page.Master.FindControl("CartControl")
     myctrl.PopulateShopCart()
    Last edited by selanec; Jan 2nd, 2010 at 11:05 AM.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: Refreshing Web User Control

    I played around for a while and i made it working. Um... i did nothing complex actually.

    I just moved the PopulateShopCart method in the PreRender event instead Load as it was previously. Now it works like a charm. Thanks a lot!

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [RESOLVED] Refreshing Web User Control

    But generally speaking you were 100&#37; right. It was about the page life cycle. Once i moved the populate method to the right step it started to loads the latest version of the shopping cart (including the newest item as well). Thank you so much mendhak!!!!

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Refreshing Web User Control

    Good enough, nice work agent.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [RESOLVED] Refreshing Web User Control

    Thanks again. Btw, i was wondering if you still use your old camera D50 or you have a new baby (hopefully you stick to Nikon if so) Your photos are excellent i can say. It goes beyond just enthusiasm.

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Refreshing Web User Control

    Why thank you

    Still on the D40. Tried working on its SDK too, but it's a real pain.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [RESOLVED] Refreshing Web User Control

    oh i remember now that you told me D40 and not D50.
    I am considering D60 at the moment as it suits my budget very best. But, i am not certain about the quality that i would possibly get buying this camera. However my favorite manufacturer is Nikon and most probably i will buy one of its models. Just still cannot make the final choice. If i had more money i know what i would buy but having this budget i will have to make some extra research this week.
    Btw, if its SDK is a real pain for you i can imagine what a pain it is going to be for me

    Sorry for the off topic guys. Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width