Tough Q on Dynamically adding cached User Controls

Discussion in 'ASP .Net' started by Justin Dutoit, Mar 19, 2008.

  1. Hi folks. I have a question on using LoadControl to dynamically add user
    controls to a page. The relevant URL is

    http://www.quickshopdemo.com/ProductGridSearchCBCacheV1.aspx?multiplesearch=bread,milk,juice

    .... and the code is below (refresh the page straightaway to see the
    problem). The page is a multiple search page, which takes several search
    terms and assigns one to each user control. The user control does a fairly
    simple search on Brand and Productname. The idea is that by having several
    simple user controls, caching could be used on each user control, for a
    faster web page.

    However, what happens is that, when the user controls are taken from the
    cache, the last search term is applied to all the user controls- e.g. search
    on bread, milk and juice, and get 'juice' for all the user controls. I am
    new to this sort of caching- I'd appreciate it if someone could see what
    might be wrong with my code.

    Thanks a lot!

    Justin.

    Code follows:

    SearchItems = Utilities.ExtractSearchItems(Request.Params("MultipleSearch"))

    ' The BrandSearchString inside this for loop are the capitalised words. The
    regular SearchString are lowercase words

    For Counter = 0 To SearchItems.Count - 1

    BrandTerms = ""

    NameTerms = ""

    Dim WordsInThisSearch() As String = Split(SearchItems(Counter), " ")

    DoSeparateBrandandProductNameWords(WordsInThisSearch, BrandTerms, NameTerms)


    '*************************************************************************

    ' Dynamically add Search user control, allowing for a cached user control

    '*************************************************************************


    Dim ctlNextSearch As Control = LoadControl("SearchCBPicsCacheV1.ascx")

    MyPlaceHolder.Controls.Add(ctlNextSearch) ' mySearchControl

    Dim mySearchControl As SearchControl = Nothing


    ' i.e. If the search user control exists (not in the cache)

    If TypeOf ctlNextSearch Is SearchControl Then


    mySearchControl = CType(ctlNextSearch, SearchControl)

    Response.Write("Is sc<BR />")

    ' i.e. if the user control is cached

    ElseIf TypeOf ctlNextSearch Is PartialCachingControl And _

    Not CType(ctlNextSearch, PartialCachingControl).CachedControl Is Nothing
    Then

    mySearchControl = CType(CType(ctlNextSearch,
    PartialCachingControl).CachedControl, SearchControl)

    Response.Write("Is pcc- BrandTerms=" & BrandTerms & ", NameTerms=" &
    NameTerms & ".<BR />")


    ElseIf TypeOf ctlNextSearch Is PartialCachingControl And _

    CType(ctlNextSearch, PartialCachingControl).CachedControl Is Nothing Then

    Response.Write("pcc.cachedcontrol Is Nothing- uc came from cache<br>")

    If mySearchControl Is Nothing Then

    Response.Write("mySearchControl Is Nothing<BR>")

    Else

    Response.Write("mySearchControl Is Not Nothing<BR>")

    End If

    Response.Write("From Cache- BrandTerms=" & BrandTerms & ", NameTerms=" &
    NameTerms & ".<BR />")

    ' when this happens, the last search item is copied over all the items, i.e.
    Bickford's

    ' which is the last search item, is in every search result

    End If


    ' Now we have mySearchControl which we can use regardless of cache or no
    cache

    If Not mySearchControl Is Nothing Then

    mySearchControl.SearchString = NameTerms

    mySearchControl.BrandSearchString = BrandTerms

    mySearchControl.SearchNumberForNext = Counter

    mySearchControl.LastSearchNumber = SearchItems.Count - 1


    End If


    Next
     
    Justin Dutoit, Mar 19, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve

    Cached User Controls

    Steve, Jul 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Karl Seguin
    Jul 7, 2003
  2. Craig G
    Replies:
    0
    Views:
    402
    Craig G
    Mar 7, 2005
  3. Dave
    Replies:
    1
    Views:
    486
  4. Savvoulidis Iordanis
    Replies:
    0
    Views:
    309
    Savvoulidis Iordanis
    Aug 15, 2008
  5. Steve

    Cached User Controls

    Steve, Jul 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    136
    Steve
    Jul 7, 2003
Loading...

Share This Page