Source view of SUBSTITUTION.ASPX
DotNet Funda: Code Viewer
substitution.aspx | substitution.aspx.cs
Close Window  
<%@ OutputCache Duration="20" VaryByParam="None" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderForTitleAndIntro" Runat="Server">
    <table width="100%" cellpadding="2" cellspacing="0">
        <tr valign="top" class="ArticleTitle">
            <td style="padding-left:10px;" valign="middle">
                asp:Substitution control</td>
        </tr>
        <tr>
            <td class="ArticleContents">
                Substitution control is used to call a method that returns a string in an output-cached page.
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderForContents" Runat="Server">
    <div class="ArticleContents">
        Ideally Substitution control is used to call a method that returns a string in an output-cached page.
        Generally it is used when you want to cache a page but still want to return a dynamic string at a paricular part of the page.
        
        <br />
        <br />
        There is one property that is used to return the dynamic string
        <table width="100%" class="TutoPropPlaceHolder" border="1" cellpadding="2" cellspacing="1">
            <tr>
                <td class="DemoCP">MethodName</td>
                <td>
                    Used to assign method name that will fire each time page will load and return the string. Please note that this method will be a static method.
                </td>
            </tr>     
        </table>   
        <!-- START - Demo Section -->
        <table class="DemoPlaceHolder" border="1" cellpadding="2" cellspacing="4">
            <tr>
                <td class="DemoTitle">
                    DEMO : Substitution
                </td>
                <td align="right">
                    <a class="DemoShowSource" href="../../misc/codeviewer/default.aspx?pagename=~/tutorials/controls/substitution.aspx" target="_blank">Show Source Code</a>
                </td>
            </tr>
            <tr style="font-weight:bold;">
                <td>
                    DateTime using Substitution: 
                    <asp:Substitution ID="Substitution1" runat="server" MethodName="WriteCurrentTimeDespiteThePageIsCahced" />
                </td>
                <td>
                    DateTime using Cachced: 
                    <asp:Label ID="lblCached" runat="server" EnableViewState="False"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                <br />
                    Notice that Cahced datetime will not change while Substituted datetime will change after each page refresh.
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <!-- START - Server Side Code -->
                    <pre>
// Substitution Control /////////////////////////////////////////
&lt;asp:Substitution ID="Substitution1" runat="server" MethodName="WriteCurrentTimeDespiteThePageIsCahced" /&gt;

 // Code Behind /////////////////////////////////////////
 protected static string WriteCurrentTimeDespiteThePageIsCahced(HttpContext context)
    {
        return DateTime.Now.ToString();
    }                    </pre> 
                    <!-- END - Server Side Code -->
                </td>
            </tr>
        </table>
        <!-- END - Demo Section -->
        </div>
    <br />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderFooter" Runat="Server">
</asp:Content>

Go Top