Tuesday 26 February 2013

Substitution Control in and calling method in .aspx page

Hi All, Today i was just glancing through the toolbox of Visual Studio, Suddenly a control came to my mind SUBSTITUTION control.

I thought to first write about this control only. Actually earlier i was planning to write about working with Nesting the GridView, But now it will be my next post..

So, This substitution control is so simple that people rarely talk about this control but it is very useful when it comes to Calling a function in your .aspx page when you have set some output cache for your page then it will help you for displaying the dynamic content on the cached page.

It is a simple solution for partial page caching for a page where most of the content is cached. In case of using this control in cached page the cached content will remain static until its cache expires but substitution control will fetch fresh data every time page is requested.

This control is basically used in pages which are cached and some part of the page needs to be updated frequently.

Lets have a look on how it works:

Simple One Line Declaration

In this line we are calling a method name DateTimeMethod delared in .aspx.cs file.

Method should meet the following criteria:

  • It should be static
  • It must accept a parameter of type HttpContext
  • It should return a string.

Best part of substitution control is that it doesnt render any markup. It will directly send the output as plain text.Other controls on page aren't accessible to Substitution control.

P.S.  Calling Csharp Methods Directly in aspx page:

One more point to note here is that we can achieve same behaviour by using script tag in aspx page. Example is as follows:

and best part of using this is we don't compulsorily have to use string as return type. We can take int or whatever we want. Given code will work absolutely fine.

No comments:

Post a Comment