Output cache does not work with postback on usercontrol - asp.net

I have a page in which i have 4 user control. 1 of them is used to filter out the the contents of the other control. I have checkboxes in that control like filter by gender, filter by size and color etc....
I have added the following code on my page for caching. it seems to work 2-3 times when you filter it like select genders checkbox and it works, if you select size filter it works, but again if you select gender filter it does not work and other control shows no results message. when i know there are results because first time it did work.
I am adding a query string parameter on each filters so that i can get the results back.
<%# OutputCache Duration="300" VaryByParam="*" %>
Query string
http://somwsite.com/jacktes/?n=0&lp=&hp=&size=&colour=&g=MENS
I have tried the following with no luck.
<%# OutputCache Duration="300" VaryByParam="none" VaryByControl="GenderFilter" %>
<%# OutputCache Duration="300" VaryByControl="GenderFilter" %> // ID of the checkbox List
<%# OutputCache Duration="300" VaryByParam="n;lp;hp;size;colour" %> // ID of the querystring param
can anyone please help me with this?
Note: No error message as page works fine BUT out of 4 control the control which shows the filter results is displaying no results.

stumbled on this while trying to find a better solution
so basically there is a workaround:
OutputCache returns invalid version with PostBack

Related

displaying session value instead of code itself in aspx

<%= Session.Item("user_fullname")%>
this supposed to display session value right?
but mine is displaying code itself....
Note: I have included aspx page in another aspx, like:
<% Response.WriteFile("../etc/header1.aspx") %>
the displaing session code is inside header1.aspx
anyone knows how to display session value?
Session items would be stored as an Object. You need to cast to the correct type:
<%= (string)Session.Item("user_fullname") %>
When using Response.WriteFile("../etc/header1.aspx"), the usual Page Life cycle will NOT be executed for the mentioned header1.aspx page.
Since there will be no asp.net page life cycle processing , the inline code
<% %> won't be executed at all and therefore all such statements will be displayed as it is.
Response.WriteFile() is used to write just the Contents of a file, which can contain HTML , Controls, directly to the output stream.

Server tags not working inside asp controls

Alrighty to make this quick:
I want to set start and end dates for a calendar extender dynamically on-change or on page load
the values are put into hidden fields on the .ascx and populated during page load in an if not postback
one set of calendar extenders is in the item template field of a grid view call this set A
the others are in a normal html table - set b
set a and set b have flags StartDate="<%# hfStart.value%>" EndDate="<%# hfEnd.value%>"
set a in the item template of a grid view column works like a charm
set b in the HTML table doesn't appear to work at all
What gives?
So far I have tried other server tags with the same code inside but I am obviously missing the salient detail. Why does one work and not the other?
UPDATE: Tried
CDate(hfstart.value).ToString with <%: and <%= tags
<%= hfstart.value %>
Unless I misunderstand, <%= will fire at the very END of the asp.net life cycle stopping it from being useful in this context.
As it turns out you DO need to use <%# %> within asp tags as others like <% %> and <%= %> execute at the end of the ASP.NET life cycle and get spit out the buffer to god knows where. When using <%# %> however, the asp control needs to be DataBound(); at the appropriate time.
This happens automatically for controls modeled in the <item template> tags in the gridview because everything within the gridview is bound on its gridview.DataBound() command.
Could it be because you're using the <%# %> tags which are for data binding? This would explain why they work in the GridView, because it supports data binding.
However in a basic HTML table you should use <% %> tags instead, or <%= %> to call a method.
For full details of the tag types, try this reference.

How to use outputcache in a usercontrol, with control properties

I have a UserControl, which should only change based on 2 URL parameters.
The problem is, it has a public property, which is used in the calling pages, so it throws a NullReferenceException on my property.
Any ideas?
I think I've figured this one out, it seems to be quite tricky which is due to my lack of comprehensive understanding of how output cache works I suspect.
You can't cache the UserControl if it has variable properties that dicatate it's content. You need to put a cache control in the Content page that holds the control. Then add the cache to the content page:
<%# OutputCache Duration="120" VaryByControl="JobList" %>
Where the vary by control is the ID of the control you wish to cache. Then specify a property for that vary by control:
<%# OutputCache Duration="120" VaryByControl="JobList.LoggedInUserID" %>
This seems to work for me!
Check the VaryBy options,
try to take a look to this articles:
http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.71%29.aspx
https://web.archive.org/web/20211020113508/https://www.4guysfromrolla.com/articles/022802-1.aspx
http://weblogs.asp.net/stefansedich/archive/2008/03/17/output-cache-with-usercontrol-on-masterpage-and-multiple-varybycustom.aspx

ASP.Net <%# %> and <%= %> rules?

Can someone explain to me the rules around what can and cannot be evaluated/inserted into markup using the <%# %> and <%= %> tags in asp.net?
When I first discovered I could inject code-behind variables into mark-up using <%= I thought 'great'. Then I discovered that if such tags are present you can then not add to the controls collection of the page (that's a whole different question!). But <%# tags are ok.
Is there a way I can inject a code-behind variable or function evaluation into the page using <%# ? Thanks.
<%%> are code blocks. You can put any server side code in them. This is a shortcut for <script runat="server"></script>.
<%=%> is for outputting strings. This is a shortcut for <script runat="server">Response.Write()</script>.
See here for more detail about <%%> and <%=%>.
<%#%> are used for data binding expressions.
See the index page for asp.net Page syntax.
The <%# inline tag is used for data binding, so if you want to use a code-behind variable inside it, you will have to bind the page or control the variable resides in:
Page.DataBind();
You can include this statement in the Page_Load or Page_PreRender event.
See this article for more information about the use of inline tags in ASP.Net, and this article for more about server-side databinding.

Debugging OutputCache in ASP.Net

I am not sure why I cannot get simple output cache to work and not sure of how to debug a situation like this.
Simply from looking at examples and previous projects I have worked on, I used the following code:
<%# OutputCache Duration="100" VaryByParam="id" %>
but since that didn't work I tried VaryByParam="*" and VaryByParam="none"
None of these worked.
Are there quick checks to make sure something else is not interfering with OutputCache?
If you're just trying to see if OutputCache is working correctly, you can always add a label to your page that just binds to the current datetime. The first time you load it, it will give you the timestamp of when the label was bound.
The next time the label should update is after the cache has expired and the page is hit again.
<%# OutputCache duration="120" varybyparam="None" varybycustom="Browser" %>
<asp:Label ID="Testlabel" runat="server" Text=""><%= DateTime.Now %></asp:Label>
You are only caching it for 100 seconds, have you tried a longer value? Starting the debugger in Visual Studio could take 100 seconds (or longer!). Also have you tried without the VaryByParam?
This is similar to womps answer with a few corrections.
If you're just trying to see if OutputCache is working correctly, you can always include the current datetime. The first time you load it, it will give you the timestamp of when the label was bound. In other words the cache is primed. If you reload the page the date should be the same as the output is cached. Once the cache is expired the date time will update again and the cycle repeats.
<%# OutputCache duration="120" varybyparam="None" %>
<%= DateTime.Now %>

Resources