Q1. How can you use Hidden frames to cache client data?

<FRAMESET cols=”100%,*,*”>
<FRAMESET rows=”100%”>
<FRAME src=”data_of_frame1.html”></FRAMESET>
<FRAME src=”data_of_hidden_frame.html”>
<FRAME src=”data_of_hidden_frame.html” frameborder=”0″ noresize
scrolling=”yes”>
</FRAMESET>

 

Q2. What are benefits and limitations of using Hidden frames?

Benefits: you can cache more than one data field, the ability to cache and access data items stored in different hidden forms, the ability to access JScript variable values stored in different frames if they come from the same site.
Limitations: hidden frames are not supported on all browsers, hidden frames data can be tampered thus creating security hole.

 

Q3. What are benefits and limitations of using Cookies?

Benefits: no server resources are required as they are stored in client, they are light weight and simple to use.
Limitations: most browsers place a 4096-byte limit on the size of a cookie, although support for 8192-byte cookies is becoming more common in the new browser and client-device versions available today, some users disable their browser or client device’s ability to receive cookies, thereby limiting the use of cookies, cookies can be tampered and thus creating a security hole, cookies can expire thus leading to inconsistency.

 

Q4. What are benefits and limitations of using Hidden frames?

Benefits: you can cache more than one data field, the ability to cache and access data items stored in different hidden forms, the ability to access JScript variable values stored in different frames if they come from the same site.
Limitations: hidden frames are not supported on all browsers, hidden frames data can be tampered thus creating security hole.

Below is sample code of implementing cookiesRequest.Cookies.Add(New HttpCookie(“name”, “user1”))

 

Asp Dot Net Caching

 

Q5. What is Query String and what are benefits and limitations of using Query Strings?

A query string is information sent to the server appended to the end of a page URL.

Benefits:no server resources are required, the query string containing in the HTTP requests for a specific URL, all browsers support query strings.
Limitations: query string data is directly visible to user thus leading to security problems, most browsers and client devices impose a 255-character limit on URL length.

Below is a sample “Login” query string passed in URL http://www.querystring.com/login.aspx?login=testing. This query string data can then be requested later by using Request.QueryString(“login”).

 

Q6. What is Absolute and Sliding expiration?

Cache.Insert(“announcement”, announcement, depends, _
DateTime.Now.AddMinutes(1), Nothing)

Sliding Expiration specifies that the cache will expire if a request is not made within a specified duration. Sliding expiration policy is useful whenever you have a large number of items that need to be cached, because this policy enables you to keep only the most frequently accessed items in memory. For example, the following code specifies that the cache will have a sliding duration of one minute. If a request is made 59 seconds after the cache is accessed, the validity of the cache would be reset to another minute:

Cache.Insert(“announcement”, announcement, depends, _
DateTime.MaxValue, _
TimeSpan.FromMinutes(1))

 

Q7. What is cross page posting?

By default, button controls in ASP.NET pages post back to the same page that contains the button, where you can write an event handler for the post. In most cases this is the desired behavior, but occasionaly you will also want to be able to post to another page in your application. The Server.Transfer method can be used to move between pages, however the URL doesn’t change. Instead, the cross page posting feature in ASP.NET 2.0 allows you to fire a normal post back to a different page in the application. In the target page, you can then access the values of server controls in the source page that initiated the post back.

To use cross page posting, you can set the PostBackUrl property of a Button, LinkButton or ImageButton control, which specifies the target page. In the target page, you can then access the PreviousPage property to retrieve values from the source page. By default, the PreviousPage property is of type Page, so you must access controls using the FindControl method. You can also enable strongly-typed access to the source page by setting the @PreviousPageType directive in the target page to the virtual path or Type name of the source page.

Here is a step-by-step guide for implementing the cross-page post back using controls that implement the IButtonControl interface.

  • Create a Web Form and insert a Button control on it using the VS .NET designer.
  • Set the button’s PostBackUrl property to the Web Form you want to post back. For instance in this case it is “nextpage.aspx” <asp:Button ID=”Button1″ runat=”server” PostBackUrl=”~/nextpage.aspx” Text=”Post to nextpage” />

When the PostBackUrl property of the IButtonControl is set, the ASP.NET framework binds the corresponding HTML element to new JavaScript function named WebForm_DoPostBackWithOptions. The corresponding HTML rendered by the ASP.NET
2.0 will look like this:

<input type=”submit” name=”Button1″ value=”Post to Page 2″ onclick=”javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(“Button1”, “,false”,”Page2.aspx”, false, false))” id=”Button1″ />

 

Q8. How do we access ViewState value of this page in the next page?

View state is page specific; it contains information about controls embedded on the particular page. ASP.NET 2.0 resolves this by embedding a hidden input field name, __POSTBACK . This field is embedded only when there is an IButtonControl on the
page and its PostBackUrl property is set to a non-null value. This field contains the view state information of the poster page. To access the view state of the poster page, you can use the new PreviousPage property of the page:

Page poster = this.PreviousPage;

Then you can find any control from the previous page and read its state:
Label posterLabel = poster.findControl(“myLabel”);
string lbl = posterLabel.Text;

This cross-page post back feature also solves the problem of posting a Form to multiple pages, because each control, in theory, can point to different post back URL.

 

Q9. Can we post and access ViewState in another application?

You can post back to any page and pages in another application, too. But if you are posting pages to another application, the PreviousPage property will return null. This is a significant restriction, as it means that if you want to use the ViewState, you are confined, for example, posting to pages in the same virtual directory. Even so, this is a highly acceptable addition to the functionality of ASP.NET.

 

Q10. How do we enable SQL Cache Dependency in ASP.NET 2.0?

Below are the steps to enable a SQL Cache Dependency:

  • Enable notifications for the database. Before you can use SQL Server cache invalidation, you need to enable notifications for the database. This task is performed with the aspnet_regsql.exe command-line utility, which is located in the c:[WinDir]Microsoft.NETFramework[Version] directory.
    aspnet_regsql -ed -E -d Northwind , where -ed : command-line switch, -E: use trusted connection , -S: specify server name it other than the current computer you are working on, -d: database name.

So now let’s try to understand what happens in the database because of “aspnet_regsql.exe”. After we execute the “aspnet_regsql -ed -E -d Northwind” command you will see one new table AspNet_SqlCacheTablesForChangeNotification and four new stored procedures created. Essentially, when a change takes place, a record is written in this table. The SQL Server polling queries this table for changes.

Here is a brief run of what the stored procedures do.

  • AspNet_SqlCacheRegisterTableStoredProcedure: This stored procedure sets a table to support notifications. This process works by adding a notification trigger to the table, which will fire when any row is inserted, deleted, or updated.
  • AspNet_SqlCacheUnRegisterTableStoredProcedure: This stored procedure takes a registered table and removes the notification trigger so that notifications won’t be generated.
  • AspNet_SqlCacheUpdateChangeIdStoredProcedure: The notification trigger calls this stored procedure to update the AspNet_SqlCacheTablesForChangeNotification table, thereby indicating that the table has changed.
  • AspNet_SqlCacheQueryRegisteredTablesStoredProcedure:This extracts just the table names from the AspNet_SqlCacheTablesForChangeNotification table. It’s used to get a quick look at all the registered tables
  • AspNet_SqlCachePollingStoredProcedure: This will get the list of changes from the
    AspNet_SqlCacheTablesForChangeNotification table. It is used to perform polling.

    • Enabling notification for individual tablesOnce the necessary stored procedure and tables are created then we have to notify saying which table needs to be enabled for notifications. That can be achieved by two ways:aspnet_regsql -et -E -d Northwind -t Products or exec spNet_SqlCacheRegisterTableStoredProcedure ‘TableName’ .

Registering tables for notification internally creates triggerfor the tables. For instance for a “products” table the following trigger is created. So any modifications done to the “Products” table will update the &quotAspNet_SqlCacheNotification” table.

“AspNet_SqlCacheTablesForChangeNotification” contains a single record for every table you’re monitoring. When you make a change in the table (such as inserting, deleting or updating a record), the change Id column is incremented by 1.ASP.NET queries this table repeatedly keeps track of the most recent changed values for every table. When this value changes in a subsequent read, ASP.NET knows that the table has changed.

  • Enable ASP.NET polling using “web.config” fileNow that all our database side is configured in order to get the SQL Cache working in the ASP.NET side we need to do some configuration in the web.config file.
    We need to set two attributes in the “web.config” file: set “Enabled” attribute to true to set the caching on and set the poll time attribute to the number of milliseconds between each poll.

Finally use the Cache dependency object in your ASP.NET code. Now comes the final step to use our cache dependency with programmatic data caching, a data source control, and output caching. For programmatic data caching, we need to create a new SqlCacheDependency and supply that to the Cache.Insert() method. In the SqlCacheDependency constructor, you supply
two strings. The first is the name of the database you defined in the element in the section of the web.config file e.g: Northwind. The second is the name of the linked table e.g: Products.
private static void CacheProductsList(List<ClsProductItem> products)
{
SqlCacheDependency sqlDependency = new SqlCacheDependency(“Northwind”,
“Products”);
HttpContext.Current.Cache.Insert(“ProductsList”, products, sqlDependency,
DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
}
private static List<ClsProductItem> GetCachedProductList()
{
return HttpContext.Current.Cache[“ProductsList”] as List<ClsProductItem>;
}

 

Q11. What is Post Cache substitution?

Post cache substitution is used when we want to cache the whole page but also need some dynamic region inside that cached page. Some examples like QuoteoftheDay,RandomPhotos, and AdRotator etc. are examples where we can implement Post Cache Substitution.

Post-cache substitution can be achieved by two means – call the new Response.WriteSubstitution method, passing it a reference to the desired substitution method callback or add a <asp:Substitution> control to the page at the desired location, and set its methodName attribute to the name of the callback method.

We pass the response substitution callback to the method. So now when ASP.NET page framework retrieves the cached page, it automatically triggers your callback method to get the dynamic content. It then inserts your content into the cached HTML of the page. Even if your page hasn’t been cached yet (for example, it’s being rendered for the first time), ASP.NET still calls your callback in the same way to get the dynamic content. So you create a method that generates some dynamic content, and by doing so you guarantee that your method is always called, and it’s content is never cached.

In case of using “<asp:substitution>” control we need to provide “methodname” attribute of the substitution control.

 

Q12. Why do we need methods to be static for Post Cache substitution?

ASP.NET should be able to call this method even when there isn’t an instance of your page class available. When your page is served from the cache, the page object isn’t created. So ASP.NET skips the page life cycle when the page is coming from cache, which means it won’t create any control objects or raise any control events. If your dynamic content depends on the values of other controls, you’ll need to use a different technique, because these control objects won’t be available to your callback.

<< Previous Page