我需要用StateView保存上次的值,但是,运行就报错,如果去掉StateView就不会有错误。
在网上搜索了说加一个什么[Serializable],但是加了也是完全没有用。
不知道哪里出现了问题,望大家指教。
[Serializable]
public partial class _Default : System.Web.UI.Page
{
RadioButton[] radYes ;
RadioButton[] radNo ;
public static int num = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
radYes =new RadioButton[47];
radNo = new RadioButton[47];
displaySource();
}
else
{
radYes = (RadioButton[])ViewState["radYes"];
}
} public void displaySource()
{
StreamReader sr = new StreamReader(Server.MapPath("~/source.txt"), Encoding.Default);
TableRow tr;
TableCell tc;
int count = 0;
while (!sr.EndOfStream)
{
count++;
string txt = sr.ReadLine();
string[] content = txt.Split('@'); tr= new TableRow();
tc = new TableCell();
tc.Text = content[0];
tr.Cells.Add(tc); radYes[count - 1] = new RadioButton();
radYes[count-1].Text = "是";
radYes[count - 1].ID = "yes" + count;
radYes[count - 1].Attributes.Add("value", content[1]);
radYes[count - 1].GroupName = "e" + count;
tc = new TableCell();
tc.Controls.Add(radYes[count - 1]);
tr.Cells.Add(tc);
radNo[count - 1] = new RadioButton();
radNo[count - 1].Text = "不是 ";
radNo[count - 1].ID = "no" + count;
radNo[count - 1].Attributes.Add("value", content[1]);
radNo[count - 1].GroupName = "e" + count;
tc = new TableCell();
tc.Controls.Add(radNo[count - 1]);
tr.Cells.Add(tc);
tblDisplay.Rows.Add(tr);
}
num = count;
ViewState["radYes"] = radYes;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//ViewState["radYes"] = counter;
int[] type = new int[9];
for (int i = 0; i < radYes.Length; i++)
{ if (radYes[i].Checked)
{
int res = int.Parse(radYes[i].ID.Substring(3, 1));
type[res - 1]++;
}
}
for (int i = 0; i < type.Length; i++)
{
Response.Write((i+1)+"号性格: "+type[i].ToString()+"<br/>");
}
}
}
在网上搜索了说加一个什么[Serializable],但是加了也是完全没有用。
不知道哪里出现了问题,望大家指教。
[Serializable]
public partial class _Default : System.Web.UI.Page
{
RadioButton[] radYes ;
RadioButton[] radNo ;
public static int num = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
radYes =new RadioButton[47];
radNo = new RadioButton[47];
displaySource();
}
else
{
radYes = (RadioButton[])ViewState["radYes"];
}
} public void displaySource()
{
StreamReader sr = new StreamReader(Server.MapPath("~/source.txt"), Encoding.Default);
TableRow tr;
TableCell tc;
int count = 0;
while (!sr.EndOfStream)
{
count++;
string txt = sr.ReadLine();
string[] content = txt.Split('@'); tr= new TableRow();
tc = new TableCell();
tc.Text = content[0];
tr.Cells.Add(tc); radYes[count - 1] = new RadioButton();
radYes[count-1].Text = "是";
radYes[count - 1].ID = "yes" + count;
radYes[count - 1].Attributes.Add("value", content[1]);
radYes[count - 1].GroupName = "e" + count;
tc = new TableCell();
tc.Controls.Add(radYes[count - 1]);
tr.Cells.Add(tc);
radNo[count - 1] = new RadioButton();
radNo[count - 1].Text = "不是 ";
radNo[count - 1].ID = "no" + count;
radNo[count - 1].Attributes.Add("value", content[1]);
radNo[count - 1].GroupName = "e" + count;
tc = new TableCell();
tc.Controls.Add(radNo[count - 1]);
tr.Cells.Add(tc);
tblDisplay.Rows.Add(tr);
}
num = count;
ViewState["radYes"] = radYes;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//ViewState["radYes"] = counter;
int[] type = new int[9];
for (int i = 0; i < radYes.Length; i++)
{ if (radYes[i].Checked)
{
int res = int.Parse(radYes[i].ID.Substring(3, 1));
type[res - 1]++;
}
}
for (int i = 0; i < type.Length; i++)
{
Response.Write((i+1)+"号性格: "+type[i].ToString()+"<br/>");
}
}
}
改成
public RadioButton[] displaySource() 返回RadioButton[]类型,不用ViewState了
这个方法是好,但别人选择了,也就是RadioButton Checked。 那又怎么将这些反应到按钮点击后的RadioButton[]中呢?