HashTable class is a serializable, it can be stored in view state without a hitch.
All the control information for the page are store in hashtable and then add the hashtable to the viewstate for the page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class viewstate : System.Web.UI.Page
{
Hashtable texttosave = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnadd_Click(object sender, EventArgs e)
{
savealltext(form1.Controls,true);
ViewState["controltext"] = texttosave;
}
private void savealltext(ControlCollection controls,bool savenested)
{
foreach (Control control in controls)
{
if (control is TextBox)
{
texttosave.Add(control.ID, ((TextBox)control).Text);
}
if ((control.Controls != null) && savenested)
{
savealltext(control.Controls, true);
}
}
}
protected void btndisplay_Click(object sender, EventArgs e)
{
if (ViewState["controltext"] != null)
{
Hashtable savedtext = (Hashtable)ViewState["controltext"];
Label1.Text = "";
foreach (DictionaryEntry item in savedtext)
{
Label1.Text += (string)item.Key+"="+(string)item.Value+"<br/>";
}
}
}
}
output
No comments:
Post a Comment