cookie執行流程,Cookie投票程序

 2023-10-08 阅读 14 评论 0

摘要:在前臺頁面先加入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"

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

?

cookie執行流程。轉載于:https://www.cnblogs.com/it-bobo/p/3899629.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/132388.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息