How ?? First of all, are you familiar with this ?
< input type="hidden" name="__VIEWSTATE" value="dDwxMzIwMTI1MjYwO3Q8O2w8aTwxPjs+O2w8dDw7bDx
pPDE+Oz47bDx0PHA8cDxsPE5hdmlnYXRlVXJsOz47b
DxXZWJmb3JtMi5hc3B4P3ZhbHVlPWJEeDBhR2x6T3o
0PTs+Pjs+Ozs+Oz4+Oz4+Oz5RXn3JAFz8V0ozE1lmu
5B1K2D7xw==" / >
If you do, that's good. It is ViewState, the hidden field that stores control state, which the state values (eg. object values, primitive type values) are serialized and deserialized into base-64 string using LosFormatter. Similarly, this approach can be used to serialize the object and pass it to the next page in query string.
The following example shows you how to pass the serialized ArrayList to the next page using LosFormatter.
Code
Firstpage.aspx
System.IO;
System.Collections;
private void Page_Load(Object sender, EventArgs e)
{
ArrayList ary = new ArrayList();
ary.Add("this");
System.Web.UI.LosFormatter formatter = new System.Web.UI.LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer,ary);
HyperLink1.NavigateUrl = "secondpage.aspx?value=" + writer.ToString();
}
Check the URL in the HyperLink control in Firstpage.aspx, it would be something like
http://localhost/WebTest/secondpage.aspx?value=bDx0aGlzOz4=
The ArrayList object has just been serialized ! How to deserialize the object back?
Secondpage.aspx
System.IO;
System.Collections;
private void Page_Load(Object sender, EventArgs e)
{
System.Web.UI.LosFormatter formatter = new System.Web.UI.LosFormatter();
ArrayList aryL = (ArrayList)formatter.Deserialize(Request.QueryString["value"]);
Response.Write(aryL[0]);
}
* Be noted, you need to explicitly specify the System.Web.UI.LosFormatter in the object instantiation. You will get the undefined Serialize()/Deserialize() method, otherwise. Watch out the number of characters supported in the URL. Not all objects are suitable to be serialized, only relatively small-size objects are appropriate to do so.
The LosFormatter class is extremely useful especially you override the SaveViewState() or LoadViewState() of the Page class/ Control, you need this class to do the serialization or deserialization of control states.
1 comment:
Thanks its works for me from colombia :)
Post a Comment