Sending xml data from one page to other page without using session state.

Posted: December 27, 2010 in ASP.Net, jQuery
Tags: , ,


Recently, I received one question how to send the xml data from one page to another page without using any state management or caching technique of Asp.Net.

Say, there are two pages page1.aspx and page2.aspx. In page1.aspx, we have one button named OpenPage2 after clicking on this button, page2 will be opened up. On page2, we want the xml data sent from page1.

I suggested the below way:

We can post the xml data from one page to another page by using XmlHttpRequest.

In page1, we can have two divs, first with id div1 for page1 and another div with id div2. On the div2, we can load page2 using jQuery’s load utility method, on click of button. Simultaneously show and hide the required div’s. When page2 has to be shown then show div2, hide div1 and vice versa. Pass xml string data to page2 using JSON object. Before sending the xml string data encode it, using JavaScript escape method.


var xmlData = escape("<Name>Rupesh</Name><Age>28</Age>");

$.load( “page2.aspx”, {“xmlkey”: xmlData } );

Hence, in the page load event of page2.aspx, the same xml string data can be extracted from the request parameters. Since, xmlData would be in encoded format in order to decode it HttpUtility.UrlDecode can be used like below.


string xmlData =  Request.Params[“xmlkey”];

xmlData = HttpUtility.UrlDecode(xmlData);

Below is the complete code.
In page1.aspx put one hidden field and on page load event of the page1.aspx store your xml data inside the hidden field so that from the client side you can get the xml data from the same hidden field and post it to page2.


//-----put below code in Page1.aspx----//

<script type="text/javascript">

var xmlData; //JavaScript object where xml string should be stored.

function openPage2 ( )  {

//Hide the div1 before opening the page2

$("#div1").hide();

//Before opening the page2 you can store the xml string in javascript object.

//The XML string can be taken from the hidden field. Use escape method to encode the data

xmlData = escape( $("#hdnXml").val() );

//Show div2 and load page2 on div2

$("#div2")

.load( "page2.aspx" , { "xmlkey" : xmlData} )

.show()

}

</script>

<div id="div1">

<input type="hidden" id="hdnXml" name="hdnXml" />

<button click="openPage2();">Open Second Page</button>

</div>

<div id =div2"></div>

//------Page1.aspx End ----

//Put below code in Page2.aspx
<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack) {

//Use urlDecode to decode the xml data

string xml = HttpUtility.UrlDecode(Request.Params["xml"]);

}

}

</script>

we could have used query string and send the data using query string also but as we know for query string there is size limit up to 256 chars. If we store the data in cookies variable then cookies also have size limit of up to 4096 Bytes and there may be the chances that cookies are not enabled in client machine.
Therefore, I suggested to use POST method in order to sending the data to the other page. The size limit for POST method depends on type of browser and IIS. I think in IIS 6 and 7 the size limit is 200KB.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s