.NETGURU
Fragment caching a custom control
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcache' list.


Joakim Sunden
-- Moved from [aspngwebcontrols] to [aspngcache] by Let the good times roll <Click here to reveal e-mail address> --

Hi.
Can someone tell me how to use fragment caching with a custom control?
With a user control it is easy, I just add the OutputCache directive to
the top of the page, but this cannot be done with a custom control.

/joakim sundén

Reply to this message...
 
    
John Crim
You can have the custom control store its output in the cache. To
grab the output, use a new HtmlTextWriter instead of the one passed into
Render.

String key; // Computed to uniquely identify the cached output for the
control

// Disclaimer: Code written without testing, probably doesn't work but
is close
protected string BuildCacheOutput()
    {
    System.IO.StringWriter stringWriter =3D new
System.IO.StringWriter();
    HtmlTextWriter newHtmlWriter =3D new HTMLTextWriter(stringWriter);

    // Do rendering
    // For this example, just call the base class Render
    base.Render(newHtmlWriter);

    // After rendering everything to the newHtmlWriter, grab the
output
    string output =3D stringWriter.ToString();

    // Add it to the cache
    DateTime tExpire =3D {compute time you want it to expire};
    HttpContext.Cache.Add(key, output, null, tExpire, 0, 0, null);

    return output;
    }

During normal rendering of your control, spit out the cached output
instead of reconstructing the output.

protected override void Render(HtmlTextWriter output)
    {
    object cachedOutput =3D HttpContext.Cache[key];
    if (cachedOutput !=3D null)
        writer.Write((string)cachedOutput);
    else
        {
        // Build cached output, cache it
        string output =3D BuildCacheOutput();

        // Then write it
        writer.Write(output);
        }
    }

Hope that helps.

John Crim
WebGecko Software
http://www.webgecko.com

-----Original Message-----
From: Joakim Sunden [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, August 08, 2002 4:54 AM
To: aspngcache
Subject: [aspngcache] Fragment caching a custom control

-- Moved from [aspngwebcontrols] to [aspngcache] by Let the good times
roll <Click here to reveal e-mail address> --

Hi.
Can someone tell me how to use fragment caching with a custom control?
With a user control it is easy, I just add the OutputCache directive to
the top of the page, but this cannot be done with a custom control.

/joakim sund=E9n
| [aspngcache] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.aspfriends.com/aspfriends/aspngcache.asp =3D JOIN/QUIT

-

-

Reply to this message...
 
    
Joakim Sunden
Thank you, but it is not exactly what I am looking for. I want to be
able to use the CacheOutput directive and its properties, such as the
VaryByParam and VaryByCustom. Can this be done?

/joakim sund=E9n

[Original message clipped]

Reply to this message...
 
    
ToddC@match.com
Sure, he is doing it manually, which is what Custom Controls are about.

To use the CacheOuput (which is probably doing the same thing his code =
is
doing ;) you must be using full pages, or User Controls.

His code could be modified to mimic the VaryByParm and VaryByCustom
functionality.

tc

-----Original Message-----
From: Joakim Sunden [mailto:Click here to reveal e-mail address]=20
Sent: Friday, August 09, 2002 1:43 AM
To: aspngcache
Subject: [aspngcache] RE: Fragment caching a custom control

Thank you, but it is not exactly what I am looking for. I want to be
able to use the CacheOutput directive and its properties, such as the
VaryByParam and VaryByCustom. Can this be done?

/joakim sund=E9n

[Original message clipped]

| [aspngcache] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/aspngcache.asp =3D JOIN/QUIT

Reply to this message...
 
    
John Crim
No, not on custom controls. =20

There are no directives for custom controls, just code. For user
controls, directives are translated into code at ASCX parse-time.

Regards,
=20
John Crim
WebGecko Software
http://www.webgecko.com

-----Original Message-----
From: Joakim Sunden [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, August 08, 2002 11:43 PM
To: aspngcache
Subject: [aspngcache] RE: Fragment caching a custom control

Thank you, but it is not exactly what I am looking for. I want to be
able to use the CacheOutput directive and its properties, such as the
VaryByParam and VaryByCustom. Can this be done?

/joakim sund=E9n

[Original message clipped]

-

Reply to this message...
 
 
System.DateTime
System.IO.StringWriter
System.Web.HttpContext
System.Web.UI.HtmlTextWriter




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification