在前台页面先加入RadioButtonList控件
if (!IsPostBack) { Per(); } } protected void btn_toupiao_Click(object sender, EventArgs e) { string P_str_IP = Request.UserHostAddress.ToString();//获取客户端IP地址 HttpCookie oldCookie = Request.Cookies["userIP"]; if (oldCookie == null) { int flag = RadioButtonList1.SelectedIndex; switch (flag) { case 0: addCount(Server.MapPath("result1.txt")); break; case 1: addCount(Server.MapPath("result2.txt")); break; case 2: addCount(Server.MapPath("result3.txt")); break; } ClientScript.RegisterStartupScript(GetType(), "", "alert(‘投票成功,谢谢参与!‘)", true); HttpCookie newCookie = new HttpCookie("userIP");//定义新的Cookie对象 newCookie.Expires = DateTime.MaxValue;//设置Cookie有效时间 //添加新的Cookie变量IPaddress,值为P_str_IP newCookie.Values.Add("IPaddress", P_str_IP); Response.AppendCookie(newCookie);//将变量写入Cookie文件中 } else { string P_str_oldIP = oldCookie.Values["IPaddress"]; if (P_str_IP.Trim() == P_str_oldIP.Trim()) { ClientScript.RegisterStartupScript(GetType(), "", "alert(‘您已经投过票了,不能再次投票,谢谢参与!‘)", true); } else { HttpCookie newCookie = new HttpCookie("userIP");//定义新的Cookie对象 newCookie.Expires = DateTime.MaxValue;//设置Cookie有效时间 //添加新的Cookie变量IPaddress,值为P_str_IP newCookie.Values.Add("IPaddress", P_str_IP); Response.AppendCookie(newCookie);//将变量写入Cookie文件中 int flag = RadioButtonList1.SelectedIndex; switch (flag) { case 0: addCount(Server.MapPath("result1.txt")); break; case 1: addCount(Server.MapPath("result2.txt")); break; case 2: addCount(Server.MapPath("result3.txt")); break; } ClientScript.RegisterStartupScript(GetType(), "", "alert(‘投票成功,谢谢参与!‘)", true); } } } //绑定投票数的百分比到前端显示 protected string M_str_rate1; protected string M_str_rate2; protected string M_str_rate3; protected int P_int_count1; protected int P_int_count2; protected int P_int_count3; public void Per() { P_int_count1 = readCount(Server.MapPath("result1.txt")); P_int_count2 = readCount(Server.MapPath("result2.txt")); P_int_count3 = readCount(Server.MapPath("result3.txt")); int P_int_count = P_int_count1 + P_int_count2 + P_int_count3; if (P_int_count == 0) { lblresult.Text = "共有0人参与投票"; } else { M_str_rate1 = (Convert.ToDouble(P_int_count1) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%"; M_str_rate2 = (Convert.ToDouble(P_int_count2) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%"; M_str_rate3 = (Convert.ToDouble(P_int_count3) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%"; lblBanana.Text = M_str_rate1; lblApple.Text = M_str_rate2; lblWatermelon.Text = M_str_rate3; lblresult.Text = "共有" + P_int_count.ToString() + "人参与投票。"; } } //读取指定路径文件中存在的投票数 public static int readCount(string P_str_path) { int P_int_count = 0; StreamReader sr; sr = File.OpenText(P_str_path); while (sr.Peek() != -1) { P_int_count = int.Parse(sr.ReadLine()); } sr.Close(); return P_int_count; } //向指定路径文件中写入投票数 public static void addCount(string P_str_path) { int P_int_count = readCount(P_str_path); P_int_count += 1; StreamWriter sw = new StreamWriter(P_str_path, false); sw.WriteLine(P_int_count); sw.Close(); }
原文:http://www.cnblogs.com/it-bobo/p/3899629.html