hashtable和viewState配合使用,用来保存页面信息,不用Session
//保存RadioButtonList选择的项
protected void Save()
{
Hashtable ht = new Hashtable(); //实例化一个Hashtable,用于储存RadioButtonList选中的值。
for (int i = 0; i < this.GridView1.Rows.Count; i++) //循环GridView每一行
{
string id = this.GridView1.DataKeys[i].Value.ToString(); //获取GridView每一行对应的id
RadioButtonList rbtn = (RadioButtonList)this.GridView1.Rows[i].FindControl("RadioButtonList1");
string matter = rbtn.SelectedValue;
if (ViewState["ht"] != null)
{
ht = (Hashtable)ViewState["ht"];
if (ht.Contains(id))
{
ht.Remove(id);
}
}
ht.Add(id, matter);
ViewState["ht"] = ht;
}
}
//还原RadioButtonList选择的项
protected void Revert()
{
Hashtable ht = (Hashtable)ViewState["ht"];
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
string id = this.GridView1.DataKeys[i].Value.ToString();
if (ht.Contains(id))
{
string matter = (string)ht[id];
RadioButtonList rbtn = (RadioButtonList)this.GridView1.Rows[i].FindControl("RadioButtonList1");
rbtn.SelectedValue = matter;
for (int j = 0; j < rbtn.Items.Count; j++)
{
if (matter == rbtn.Items[j].Value)
{
this.GridView1.Rows[i].ForeColor = Color.Red;
}
}
}
}
}