My Programer Life

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
    //投票
    protected void btn_Vote_Click(object sender, EventArgs e)
    {
        string userIpAddress = Request.UserHostAddress.ToString();//获取用户本地的IP地址
        HttpCookie oldCookie=Request.Cookies["UserIP"];//创建Cookie对象
        if (oldCookie == null)//该用户没有投票记录
        {
            int flag = this.rbl_Evaluate.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;
                default:
                    break;
            }
            Page.RegisterClientScriptBlock("", "<script>alert('投票成功,谢谢您的参与')<script>");
            HttpCookie newCookie = new HttpCookie("UserIP");//创建新的Cookie对象
            newCookie.Expires = DateTime.MaxValue;//设置Cookie的有效时间
            newCookie.Values.Add("IPAddress", userIpAddress);//添加用户的IP地址在Cookie中,保存起来
            Response.AppendCookie(newCookie);//将变量写入Cookie文件中
        }
        else
        {
            string userOldAddress = oldCookie.Values["IPAddress"].ToString();
            if (userIpAddress.Trim() == userOldAddress.Trim())//创建Cookie对象
            {
                Page.RegisterClientScriptBlock("", "<script>alert('每天只能投票一次,谢谢您的光顾!!')</script>");
            }
            else
            {
                HttpCookie newCookie = new HttpCookie("UserIP");//创建新的Cookie对象
                newCookie.Values.Add("IpAddress", userIpAddress);
                newCookie.Expires = DateTime.MaxValue;
                Response.AppendCookie(newCookie);
                int flag = this.rbl_Evaluate.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;
                    default:
                        break;
                }
                Page.RegisterClientScriptBlock("", "<script>alert('投票成功谢谢您的参与')</script>");
            }
        }
    }

    /// <summary>
    /// 读取文本文件中的数字
    /// </summary>
    /// <param name="str_path">文本文件的路径</param>
    /// <returns>返回结果</returns>
    protected static int ReadCount(string str_path)
    {
        int count = 0;//创建整形的变量
        System.IO.StreamReader srd;//创建流读取对象
        srd = System.IO.File.OpenText(str_path);//打开指定的文本文件
        if (srd.Peek()!=-1)//如果有数据
        {
            count = int.Parse(srd.ReadLine());//读取文本文件的数字赋值给count
        }
        srd.Close();//关闭流对象
        return count;  
    }

      

    /// <summary>
    /// 对现有的投票数进行增加
    /// </summary>
    /// <param name="str_path">文本文件的路径</param>
    protected static void AddCount(string str_path)
    {
        int count = ReadCount(str_path);
        count += 1;
        System.IO.StreamWriter swr = new System.IO.StreamWriter(str_path);//将数据的记录写入文件
        swr.WriteLine(count);
        swr.Close();
    }

 



 

posted on 2014-03-12 20:37  My Programer Life  阅读(480)  评论(0编辑  收藏  举报