//保存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");
int matter = rbtn.SelectedIndex;
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))
{
int matter =(int)ht[id];
RadioButtonList rbtn = (RadioButtonList)this.GridView1.Rows[i].FindControl("RadioButtonList1");
rbtn.SelectedIndex = matter;
}
}
}