Cookie voting Program

Source: Internet
Author: User

Add the radiobuttonlist control to the front-end page.

If (! Ispostback) {per () ;}} protected void btn_toupiao_click (Object sender, eventargs e) {string p_str_ip = request. userhostaddress. tostring (); // obtain the Client IP address 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; CA Se 2: addcount (server. mappath ("result3.txt"); break;} clientscript. registerstartupscript (GetType (), "", "alert ('vote successful, thank you! ') ", True); httpcookie newcookie = new httpcookie (" userip "); // defines the new Cookie object newcookie. expires = datetime. maxvalue; // set the cookie validity period // Add a new Cookie variable IPaddress with the value p_str_ip newcookie. values. add ("IPaddress", p_str_ip); response. appendcookie (newcookie); // write the variable to the cookie file} else {string p_str_oldip = oldcookie. values ["IPaddress"]; If (p_str_ip.trim () = p_str_oldip.trim () {clientscript. registerstartupscript (Ge Ttype (), "", "alert ('You have voted. You cannot vote again. Thank you! ') ", True) ;}else {httpcookie newcookie = new httpcookie (" userip "); // defines the new Cookie object newcookie. expires = datetime. maxvalue; // set the cookie validity period // Add a new Cookie variable IPaddress with the value p_str_ip newcookie. values. add ("IPaddress", p_str_ip); response. appendcookie (newcookie); // write the variable to the cookie file int flag = radiobuttonlist1.selectedindex; Switch (FLAG) {Case 0: addcount (server. mappath ("result1.txt"); break; Case 1: addcount (SE Rver. mappath ("result2.txt"); break; Case 2: addcount (server. mappath ("result3.txt"); break;} clientscript. registerstartupscript (GetType (), "", "alert ('vote successful. Thank you for participating! ') ", True) ;}}// display protected string m_str_rate1; protected string m_str_rate3; protected int p_int_count1; protected int p_int_count2 to the front end; 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"); I NT p_int_count = p_int_count1 + p_int_count2 + p_int_count3; If (p_int_count = 0) {lblresult. TEXT = "0 participants in voting";} 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/conve Rt. todouble (p_int_count )). tostring ("0.00") + "%"; lblbanana. TEXT = m_str_rate1; lblapple. TEXT = m_str_rate2; lblwatermelon. TEXT = m_str_rate3; lblresult. TEXT = "Total" + p_int_count.tostring () + "Participants vote. ";}}// Read the number of votes in the specified path file. 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;} // Number of votes written to the specified path file 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 ();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.