Pass an encoded XML/HTML content in the form of Byte Array from one page to another

Niladri.Biswas
Posted by in ASP.NET category on for Beginner level | Points: 200 | Views : 7917 red flag

In this article we will look into "how to pass an encoded XML/HTML content as Byte Array from one page to another using a query string"


 Download source code for Pass an encoded XML/HTML content in the form of Byte Array from one page to another

Introduction

In this article we will look into as how to pass an encoded XML/HTML content as Byte Array from one page to another using a query string

Straight to Application Development

First create a page call as "Page1.aspx" with a multiline textbox control and a button control as under

Now in the button click event write the below piece of code

	protected void btnSubmit_Click(object sender, EventArgs e)
{
//Get the string data as byte array
var byteChunk = System.Text.Encoding.UTF8.GetBytes(txtInput.Text);

//Encode the data
var encoded = HttpUtility.UrlEncode(Convert.ToBase64String(byteChunk));

//Pass the data
Response.Redirect("Page2.aspx?Data=" + encoded, false);
}

Create another page call "Page2.aspx" with a multiline textbox control and it should be read only where we will display the data

Write the below code in the Page load event of the "Page2"

	protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var byteChunk = Convert.FromBase64String(Request.QueryString["Data"]);
txtDisplay.Text = Encoding.UTF8.GetString(byteChunk);
}
}

Time to test

Enter some Html content in the TextBox of Page1.aspx and click on the Submit Button

The output will appear as under

Practical Implementation

We had a recent requirement where I need to pass some Xml content based on certain condition in a cross post back situation.There was one more need as the data could be accessed by other applications like Silverlight, WPF etc. in which case we had no choice except using a query string for data communication.We encoded the data in byte array and was passing it via the query string and reading it back in string format in the target page.

Conclusion

In this article we have seen as how to encode a Html content as byte array and cross post it between pages and convert it back to the original Html content from the byte array

Hope this will be useful

N.B.~Kindly note that we cannot use a Query String approach while posting a huge chunk of data.We need to POST the data in that case which we will cover up in some other article.The maximum size/length of data is browser dependent.For more information, please refer WWW FAQs: What is the maximum length of a URL?

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)