会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
博客园
博客园
首页
新随笔
联系
订阅
管理
Serializer for dynamic control
using
System;
using
System.IO;
using
System.Collections;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.Runtime.Serialization;
using
System.Web;
using
System.Text;
public
class
Serializer
{
"Serialize"
#region
"Serialize"
public
static
byte
[] Serialize(Hashtable h)
{
MemoryStream ms
=
new
MemoryStream();
BinaryFormatter bf
=
new
BinaryFormatter();
try
{
bf.Serialize(ms, h);
}
finally
{
ms.Close();
}
return
ms.ToArray();
}
#endregion
"Deserialize"
#region
"Deserialize"
public
static
object
Deserialize(
byte
[] bytes)
{
MemoryStream ms
=
new
MemoryStream();
BinaryFormatter bf
=
new
BinaryFormatter();
try
{
ms.Write(bytes,
0
, bytes.Length);
ms.Seek(
0
,
0
);
object
o
=
bf.Deserialize(ms);
ms.Close();
return
o;
}
catch
{
return
null
;
}
finally
{
ms.Close();
}
}
#endregion
"WebSerialize"
#region
"WebSerialize"
public
static
string
WebSerialize(Hashtable h)
{
return
HttpContext.Current.Server.UrlEncode(Encoding.Default.GetString(Serialize(h)));
}
#endregion
"WebDeserialize"
#region
"WebDeserialize"
public
static
object
WebDeserialize(
string
content)
{
return
Deserialize(Encoding.Default.GetBytes(HttpContext.Current.Server.UrlDecode(content)));
}
#endregion
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
#region "OnInit"
protected override void OnInit(EventArgs e)
{
//base.OnInit(e);
//string inform = Request.Form[hfHolder.ClientID];
//if (!string.IsNullOrEmpty(inform))
//{
// string[] array = inform.Split(';');
// string type = array[0];
// string id = array[1];
// if (type == "TextBox")
// {
// TextBox tb = new TextBox();
// tb.ID = id;
// PlaceHolder1.Controls.Add(tb);
// }
//}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(hfHolder.Value))
{
Hashtable h = Serializer.WebDeserialize(hfHolder.Value) as Hashtable;
Response.Write(h["test"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//if (string.IsNullOrEmpty(hfHolder.Value))
//{
// TextBox tb = new TextBox();
// tb.ID = "test";
// PlaceHolder1.Controls.Add(tb);
// hfHolder.Value = "TextBox;test";
//}
}
protected void Button2_Click(object sender, EventArgs e)
{
Hashtable h = new Hashtable();
h.Add("test", "007");
hfHolder.Value = Serializer.WebSerialize(h);
}
}
posted @
2008-06-19 23:59
N/A2011
阅读(
199
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告