asp.net user control (using caching) __.net

Source: Internet
Author: User

The ASP.net caching feature is a very important feature, and most of this is the use of fragment caching implemented by user controls.

Sometimes it is impractical to cache an entire page, and some parts of the page may need to be created dynamically for each request. In these cases, it may be worthwhile to take the time to identify the object or data associated with a page request that requires a large number of server resources to be constructed. Once these items are identified, you can save server resources by creating these items in a Web forms user control to separate them from the rest of the page and then caching them for a specified amount of time. This is what is commonly said as fragment caching. This technique allows you to separate portions of the page that you need to spend valuable processor time creating, such as database queries, with other parts of the page. You can choose to allow a portion of the page that requires fewer server resources to be dynamically generated for each request.

For the user control to have caching capabilities, you can add @OutoutCache instructions at the top of the. ascx file, and the details of this directive are available in this section of the SDK documentation: MS-HELP://MS. Netframeworksdkv1.1.chs/cpgenref/html/cpconoutputcache.htm
Here's a look at the usual ways:

<%@ OutputCache duration= "All" varybyparam= "None"%>
The above is to cache this control for 30 seconds, and the cached content does not change with any of the specified parameters.

Here we can use this control, as the usual practice is to declare it with @Register directives on the. aspx page and add it to the page. If you just want to do the whole job on the. aspx page, it's all over.
<user:timex id= "Uctime" runat= "Server" ></user:timex>

But if you want to use the properties of a control in codebehind, I mean to specify attributes in a. cs file, or to load a user control dynamically, it takes a little more effort.
First, use this cached control with the codebehind.

Uctime.times=system.datetime.now.tostring ();

The Times is a property of the control that specifies the text value of the label in the control. This page does not have any problems when it is first run, but when you refresh the page, an error occurs, prompting that the control does not exist at all. You can also run the example dynamically, and the same error will occur.
If the user control specified for the output cache is declared and used in the page, the ASP.net page parser instantiates one of the two object types based on how the user control was created. If the user control is created declaratively, the StaticPartialCachingControl object is added, and if the user control is created programmatically using the LoadControl method, the PartialCachingControl object is added

It means that once you use a user control with Outoutcache content, the system creates a new cache object based on the form of the control, and then uses the new cache object throughout the cache cycle instead of using the user control itself. This means that the page is initially loaded with the user control itself, followed by the cached object used in the cache cycle. This is why the error occurs above.

Find the reason, solve the problem is much simpler. We can add a judgment to the. CS and then assign the properties to the user control itself and the cached object separately. (here to say the method is simple, I in order to find this answer can be a great effort, but the final or csdn of high man to help me solve, hehe)
This is also the score page declaration and dynamic loading of the two categories.
First look at the page declaration when the practice:

protected Prac2.uc_cache uctime;
private void Page_Load (object sender, System.EventArgs e)
{
Set ();
}

private void Set ()
{
if (uctime!=null)
{
Uctime.times=system.datetime.now.tostring ();
}
}
The control is declared in the codebehind, and then when the property is used, it is the initial load if the control is not empty. Assign a value to a property at this time. This way, when the page is loaded again, the process does not go in because the cache is used. However, the attribute value still exists.
Then look at the dynamic loading approach:
protected System.Web.UI.WebControls.PlaceHolder placetime;
private void Page_Load (object sender, System.EventArgs e)
{
Loaduc ();
}
private void Loaduc ()
{
First loading the control dynamically, its type is obviously uc_cache when it is first loaded
Control Ucc=loadcontrol ("Uc_cache.ascx");
Placetime. Controls.Add (UCC);
Prac2.uc_cache Uctime=null;
First-time processes executed
If (UCC is Prac2.uc_cache)
{
Uctime= (Prac2.uc_cache) UCC;
}
Second and subsequent processes for the entire cache cycle
else if (UCC is partialcachingcontrol&& (PartialCachingControl) UCC). Cachedcontrol!=null)
{
Uctime= (Prac2.uc_cache) (PartialCachingControl) UCC). Cachedcontrol;
}
Finally, assign a value to a property
if (uctime!=null)
{
Uctime.times=system.datetime.now.tostring ();
}
}

This gives a clearer picture of how the caching control works by loading a control and then manipulating it separately based on whether the user control itself or the cached control, and finally assigning the property to the control. This program completes the operation of the control properties by using an intermediate amount. UCC is the dynamically loaded control, the first time for the user control, and to the second, it becomes a caching control, the program for these two changes to the different operations, the final result is the UCC correctly assigned to the Uctime , and finally uctime to manipulate attributes.

In the 13th article of the reprint essence is also the discussion which the content launches, actually I read this article only then has understood the PartialCachingControl usage.
I am in the post of CSDN position, interested can go to see:

http://community.csdn.net/Expert/TopicView.asp?id=3138685

Here, the basic content of the user control should end. In the future practice, should strive to learn how to better use this powerful technology-user control.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.