在前臺頁面先加入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_IPnewCookie.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_IPnewCookie.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();}
?