I have created a user control in my application "header.ascx", I am pasing a selectedMenu attribute to this control on which the control selects the selectedMenu value specified. Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu.
I want to cache this control, When the value of the selectedMenu attribute changes then only the cache will be refreshed else it should picks up the control from cache??
Is it possible to cache a user control in asp.net?? I am using ASP.NET 2.0 (C#)
User control caching in ASP.NET is called fragment caching. It's done by adding an OutputCache directive to the top of your page:
You can't vary the cache by setting the property on the control because the control isn't actually created if it's found in the cache. If you try to access the control in the code behind it's cached, it will be null.
Is the condition that determines whether the control should be cached or not something that you can determine by looking at the current request? If it is, you can use the varybycustom attribute (http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx) of the output cache directive. You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxa will be called and you can put the logic for whether the control should be cached or not there.
Of course you can! It's called "Fragment Caching". Here's a link to the Quickstarts page and the MS Knowledge base. Additionally, Google.
I don't think it's a good idea to cache the control itself:
When the control is created the very first time, it has references to its parent page among others.
When you retrieve the control from the cache, those references does no longer exists.
A better approach, I think, is to cache the data which the control is using instead. ASP.NET creates so many controls during the page life cycle, that caching this one control really doesn't improve anything.
Then a stupid question at the end: Is this control a bottleneck? Do you really need the cache?
To summarize
using VaryByCustom, means
1- Build the control again.
2- Having multiple versions of the control in the cache. which will be used when the custom conditions meet.
This is only good if the control is taking a lot of time to build and we have finite number of cached versions to not waste memory, and the application will need to access control properties (while it is cached "or null").
but it will not be good if that custom conditions are depending on the control properties itself. I can't access it, it is null.
for example I want to write something like
if (the default selected value in countries list is NOT USA)
then rebuild and Cache (give it a different string)
Otherwise don't
while other objects are trying to access the contries list, it will check for null, and set the countries drop down list to USA.
The data cahing will do the work. it is the only way.
who agree?
Thanks for ur time
Related
If this is too hypothetical and needs to be somewhere else, please let me know.
I have a project that needs specific gridviews to appear on multiple pages. Instead of copying and pasting the gridviews on each of the pages I thought creating a user control for each specific gridview or create a page for each gridview and then use iframes would be my best options.
I have not used either extensively so I am looking to the SO community's experience, are there known problems with using user controls and/or iframes when it comes to:
validation
communication between user control/iframe and parent page
ajax/updatepanels containing user control/iframe
thanks in advance
Since a gridview is essentially a user control (that's very flexible), I would first explore doing this with neither of your options. If possible, use the standard gridview and let your data layer do most the work. However, assuming you already know that... but your requirements require one or the other of your options, here are things to consider.
An iframe is easy to implement but unless your need is really simplistic in terms of user interaction, the user control will be the most flexible. Another downside to an iframe is it's size (you're essentially loading two pages). The downside of user controls are the upfront time in building them.
Based on your criterion:
1. validation - can do with either option but you'll have more flexibility with a user control
2. communication between user control/iframe and parent page - much easier with user control unless query string parms will do the trick
3. ajax/updatepanels containing user control/iframe - again user control
I have created dynamic menu on the basis of user's permissions and module assigned, which has loop and if - else statements.
This menu creation is in Header.ascx which is included in all the pages.
So menu creation logic executes on every page load. I want to avoid this execution on every page.
Any good suggestions to improve my implementation.
Note: menu is user specific.
If the code is already inside a user control, you can use ASP.NET User Control caching:
http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx(link no longer valid)
http://msdn.microsoft.com/en-us/library/h30h475z(v=vs.100).aspx (updated link)
Why not store the generated menu inside of the session, and only build the menu if the session value isn't present? This will increase memory consumption by your application, but increase runtime performance.
In some of our applications, we use:
A session (app that have no sessions, only this one) to keep our control menu in memory or;
A session to keep your data, and just build your menu, without get your data every postback or;
We just do like you and put it on the Page_Load.
This will depend on how you are getting your data and how big are this data.
I have a search button in a asp.net page that show a pop-up and by closing that some controls are be added to a panel in my form using AjaxManager of Telerik.
I've used the Telerik:AjaxUpdatedControl to say render the panel everytime a search is done.
But my proble is when search is done for second time the controls that had been adde for first time will be removed.
how can I overcome this problem?
Dynamic controls are not created after a postback. You have to recreate them every single time. (Their viewstate is still maintained you just have to create them with the same ID and in the same place within the control hierarchy).
The DynamicControlsPlaceholder control does this all for you. It is available at - http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
However I would suggest you use it for as little as possible. Heavy usage of it can cause performance to suffer.
We have an application where we have a single level navigation menu with some heavy-duty pages on each link. The user can switch back and forth between these pages frequently to obtain information that he needs.
Once the page gets generated, it wouldn't change for the session. However, the page is specific to the user, hence we cant cache it.
I was trying to come up with a solution where we generate the page once, and keep it hidden in the background until its link is clicked, but haven't been able to get my head around this.
One of the ways I thought was to have multiple div tags (one for each page) on one page and keep toggling the visibility as the links are pressed, but that would end up making this single page very heavy. Someone also suggested using iFrames, but I am not really comfortable using the iFrames much and I'm not even sure, if it would be any helpful either.
Can you guys please suggest a few approaches to tackle the issue?
update: Just to clarify, we are fine with keeping the pages separate and navigate across using a standard menu bar. We were just looking for ways to optimize the performance as we know that the pages once generated wouldn't change and there should be some way to tap that benefit.
You can use Ajax tab control for this purpose
Try taking a look at this MSDN article which specifically tackles the issue of how to user-level cache. Also, it might be more manageable to break each tab into a user control. That way your ASP.NET page has just the tab control and 1 user control for each section under the tab. It makes managing tabs much easier.
EDIT:
What I would do in your case, since you say the data won't change for the user, is I would grab the static data from the database and then I would store that data in the Session cache. THe session cache is specific per user and you can try to retrieve the static data from there instead of repetitively calling the database.
Check out the ASP Multiview control. Just remember that even though the different views are hidden when not active, their viewstate is still being sent back and forth. Can be a benefit if you need to check control values across views though.
I am struggling with finding clear answers to dynamically creating the same page over and over. The questions and samples I have found seem to be all over the board on this topic. I have studied the life cycle and still seem to not have a clear answer as to where code should go.
I have a master page and a content page. All the content in the content area needs to be dynamically created (text boxes, ddl's, page tabs, buttons/onclick etc.).
After a user fills in data and clicks a submit button, I need to read the values off the form and rebuild the page completely again (not add/remove controls to current content).
My question is then.
Where do I put my code to build the page?
Will this area allow me to use IsPostBack so I can rebuild content with Request.Form values?
Will my buttons _Click events work?
Are there any working samples out there you could direct me to?
Thank you very much for the feedback...
I don't know all the answers to your questions, but I hope this may get you started. When dynamically generating the UI through code, this happens in Init. Controls dynamically loaded on Init is key because between init and load, on postback, viewstate is loaded for these controls.
This means you need, on every postback, recreate the page as is to match the previous control tree, then deconstruct it after init and recreate the new UI, if something is supposed to change UI wise. This is because it validates the tree structure to determine its the same UI. Now, if you don't need viewstate, this may not be as much of an issue. I haven't verified this without viewstate to see if it behaves different.
It depends how dynamic you need it, whether you need viewstate (is a big factor).
HTH.
Try creating the controls in the page's PreInit method. "IsPostBack" should work and the click event handlers should work as well.
What you need is a web user control, see ASP.NET User Controls
Brian's advices are good and you should follow them.
This might not really answer your question but still I add it as an advice. I'm professionally creating ASP.net web applications at quite a large scale and from my experience I can say that too much "dynamics" is usually bad and should be avoided because it just introduces complexity. Normally you might want to expose UI parts into ASP.net UserControls or if you want to make them even more reusable (if that's a factor) then into ASP.net Server controls. Then you replace different of them dynamically rather than creating everything from scratch.