J
Justin Dutoit
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
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