Introduction:
This tutorial illustrates how to Implement Fragment Caching, this caching is conceptually same as Page caching. Here we will create a User Control file [.ascx] which will show System Date & Time in a Label control and embed it in a aspx.page that will also show system's Current Date & Time.
We will use Directive in the User control file and Directive in our Main Page to embed User Control file below Directive.
Same as earlier we have done in Page Cache we have to declare it's Duration attribute, when you request the Main Page label in Main page gets updated but the Label in User Control file is cached hence it will show the cached time (assuming that the duration of User Control file has not elapsed).
Step 1:
Create a Website name it Caching and rename the Default.aspx file to FragmentCache.aspx
Step 2:
Drag & Drop Label control on the Page with properties [ID= LblMsg ]
Step 3:
Write this code in FragmentCache.aspx.cs File.
protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Current Time : "; LblMsg.Text += DateTime.Now.ToString(); }
The above code is for Main Page which will show system current Date & Time.
Step 4:
Now add a new Web User Control file in this project and name it UCPage.ascx
Step 5:
Again Drag & Drop Label control on the UCPage.ascx with properties [ID= LblMsg ]
Step 6:
Write this Line of code under Directive in PageCache.aspx File.
The above code will make this file cached for 10 secs.
Step 7:
Now Write This Line of Code in UCPage.ascx.cs File
protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Cached Time : "; LblMsg.Text += DateTime.Now.ToString(); }
Step 8:
Now Drag & drop UCPage.acsx File from solution explorer on FragmentCache.aspx file by doing this, automatically these line of code are added in FragmentCache.aspx file.
When you debug FragmentCache.aspx page you will see that the page shows the current time in upper Label control while lower Label Control shows Cached time. Whether how many time you have refresh the page within 10 secs in will show same time, as soon as the time elapsed you will get the current system time in both Controls. This is how we implement Fragment cache in asp.net
[4:26 PM
|
0
comments
]
0 comments
Post a Comment