you're reading...

Hour 2: Understanding 5 ASP.NET State management techniques in 5 hours

Query String:

Points to remember: Benefits

1)    Used for passing information using a query string in the URL

2)    The query string is the portion of the URL after the question mark

3)    Query strings are lightweight and don’t exert any kind of burden on the server.

4)    Best suited in e-Commerce scenario

Points to remember: Disadvantages

1) They can only contain simple string and they should only contain URL pre-defined accepted characters.

2) Information is visible and can be tampered. Values can be changed which could not be handled by the application at receiving it

3)    We cannot put large amount of data in query string, as there is size limitation enforced by different browsers, usually from 1 to 2 Kb.

How to send value in query string

Response.Redirect(“QueryStringRecipient.aspx” + “?Version=” +


How to receive value from query string

lblDate.Text = “The time is now:<br>” + DateTime.Now.ToString();

switch (Request.QueryString[“Version”])


case “cmdLarge”:

lblDate.Font.Size = FontUnit.XLarge;


case “cmdNormal”:

lblDate.Font.Size = FontUnit.Large;


case “cmdSmall”:

lblDate.Font.Size = FontUnit.Small;



What is URL encoding: In query string, there is limitation in allowing valid characters. With URL encoding, special characters are replaced by escaped character sequences starting with the percent sign (%), followed by a two-digit hexadecimal representation.

When to use it: when we want that the data should store in the query string may not consist of URL legal characters, you should use URL encoding.

These special characters have special meaning

1)    (&) is used to separate multiple query string parameters,

1)    (+) is an alternate way to represent a space

2)    (#) is used to point to a specific bookmark in a web page.


string productName = “Apple iPad”;

Response.Redirect(“newpage.aspx?productName=” + Server.UrlEncode(productName));

Here we have used method UrlEncode from HttpServerUtility class. URL encloding here we are encoding a string of arbitrary data for use in the query string. This replaces all the non legal characters with escaped character sequences.

HttpServerUtility.UrlDecode() method can be used to  return a URL-encoded string to its initial value.

Cross Page Posting

Description: Till here we learned that page postbacks to themselves, and when they do it send back the current controls in the form for that page which also includes content of hidden variables value.

How it works: PostBackUrl property of button support cross page post backs. When the user clicks the button, the page will be posted to that new URL with the values from all the input controls on the current page.

Scenario #1: How to Set CrossPostBack attribute

<asp:Button runat=”server” ID=”cmdPost” PostBackUrl=”CrossPageTarget.aspx”

Text=”Cross-Page Postback”  />

Scenario #2: How to get source page info on target page.

In CrossPageTarget.aspx, the page can interact with the CrossPageSpurce.aspx objects using the Page.PreviousPage property.

if (Page.PreviousPage != null)


lblInfo.Text = “You came from a page titled ” +



But if we are interested into more specific details, such as control values, you need to cast the PreviousPage reference to the appropriate type.

CrossPageSource prevPage = PreviousPage as CrossPageSource;

Just by casting the previous page to the appropriate page type, we still won’t be able to directly access the control values. That’s because the controls are declared as protected members. You can handle this by adding properties to the page class that wrap the control variables

In Source page, create a property like this;

public string TextBoxContent


get { return txt1.Text; }


And in target page write below code to access previous page control value

if (prevPage != null)


lbl.Text += “You typed in this: ” + prevPage.TextBoxContent + “<br />”;


Scenario #3: How to keep control view state inact without using CrossPagePostBack.

Solution; Server.Transfer , because we need some work around , because only button control implements iButton iterface have this attribute , so in case you don’t have any button  , you can use server.tranfer overloaded method

Server.Transfer(“CrossPageTarget.aspx”, true);

Scenario #4: How to check between a cross-page post that’s initiated directly through a button and the Server.Transfer() method

if (PreviousPage.IsCrossPagePostBack)


lbl.Text += “The page was posted directly”;




lbl.Text += “You used Server.Transfer()”;


Scenario #5: How Page.IsPostBack work in Cross Page PostBack.

For the source page (the one that triggered the cross-page postback), the IsPostBack property is true. For the destination page (the one that’s receiving the postback), the IsPostBack property is false.

How it works: once we navigate from source page to target page, and from target page calls page.previouspage , life cycle for source page starts and Page.load event is fired. Page.IsPostBack property on source page is true so what happens is your code skips the time-consuming initialization

steps. Instead, the control values are restored from view state.  On the other hand, the Page.IsPostBack property for target page is false, so this page performs the necessary first-time


if (IsCrossPagePostBack)


// This page triggered a postback to CrossPage2.aspx.

// Don’t perform time-consuming initialization unless it affects

// the properties that the target page will read.


else if (IsPostBack)


// This page was posted back normally.

// Don’t do the first-request initialization.




// This is the first request for the page.

// Perform all the required initialization.


Scenario #6: How Validation controls works in Cross Page Postings.

We are looking at scenario where user click the button and validations doesn’t happens for any reason say client side scripting is not supported. So there should be an way out by which we can test it from target page.

if (Page.PreviousPage != null)


if (!PreviousPage.IsValid)


Response.Redirect(Request.UrlReferrer.AbsolutePath + “?err=true”);




So now source page need to know presence of this query string value and perform the validation. So on source page we can something like this;

protected void Page_Load(object sender, EventArgs e)


if (Request.QueryString[“err”] != null)



To test this set RequiredFieldValidator.EnableClientScript property to false.

This is end of your 2nd hour of reading Hope you enjoyed it.Click to go on 3rd hour of reading


About Vishal

Vishal Nayan is a seasoned professional with hand on Experience on Mircrosoft Technologies. He always look for challenging IT position that allows him to learn new Microsoft Technologies while utilizing experience of Project Development and Software Engineering Ethics. A MCP in WCF ,and looking forward for more.


One thought on “Hour 2: Understanding 5 ASP.NET State management techniques in 5 hours

  1. Thxs Vishal u r articles are gud n better understanding way.

    Posted by gopi | 2012/07/05, 3:33 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


Follow Vishalnayan on WordPress.com

Blog Stats

  • 217,514 hits


%d bloggers like this: