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"
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)
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
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.
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
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?