out的作用

来源:互联网 发布:最常用编程语言 编辑:程序博客网 时间:2024/04/29 02:58

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   this.btnSumbitVote.Attributes.Add("onclick","return checkForm()");

   this.VoteId = Request["id"]+""; 
   if(this.VoteId== string.Empty)
   {
    Response.End();
   }
   else if((Request.UrlReferrer+"").ToString().ToLower().IndexOf("event/ishow/2006/")<0)
   {
    Response.End();
   }
   else if(Request.Cookies["vote"] != null && Validate_Cookies(Request.Cookies["vote"].Value+"", this.VoteId))
   {
    Response.Write(@"<script>alert(""10分钟内不能重复投票同一作品!""); history.back();</script>");
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.btnSumbitVote.Click += new System.EventHandler(this.btnSumbitVote_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  public bool Validate_Cookies(string oldstring,string voteId)
  {
   bool result=false;
   string[] arr = oldstring.Split(',');
   for(int i=0;i<arr.Length;i++)
   {
    if(arr[i]==voteId)
     result = true;
   }
   return result;
  }

  public bool ValidateInput(out string errStr)
  {
   if(this.UserNameValue ==string.Empty)
   {
    errStr = "用户名不能为空。";
   }
   else if (this.MobileValue == string.Empty)
   {
    errStr = "手机号不能为空。";
   }
   else if (this.ValidateMobile(this.MobileValue))
   {
    errStr = "一个手机号只能投一次票。";
   }
   else if(this.PassWordValue == string.Empty)
   {
    errStr = "学校代码不能为空";
   }
   else if(!this.ValidatePWD(this.VoteId, this.PassWordValue))
   {
    errStr = "学校代码输入不正确。";
   }
   else if(this.CheckCodeValue == string.Empty)
   {
    errStr = "验证码不能为空。";
   }
   else if(this.CheckCodeValue.ToUpper() != Session["chkcode"]+"")
   {
    errStr = "验证码输入不正确。";
   }
   else
   {
    errStr = string.Empty;
    return true;
   }
   return false;
  }
  public bool ValidatePWD(string voteid, string finalpassword)
  {
   //密码存在返回true;
   string strSql="SELECT * FROM vote WHERE id='"+ voteid +"' AND finalpassword='"+ finalpassword +"'";
   
   string connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
   SqlConnection sqlConn = new SqlConnection(connStr);

   SqlCommand sqlCmd = new SqlCommand(strSql,sqlConn);
   sqlConn.Open();
   SqlDataReader dr = sqlCmd.ExecuteReader();
   if(dr.Read())
   { 
    return true;
   }
   else
   {
    return false;
   }
  }

  public bool ValidateMobile(string mobile)
  {
   //手机已存在返回true;
   string strSql="SELECT * FROM votefinal WHERE telephone ='"+ mobile +"'";
   
   string connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
   SqlConnection sqlConn = new SqlConnection(connStr);

   SqlCommand sqlCmd = new SqlCommand(strSql,sqlConn);
   sqlConn.Open();
   SqlDataReader dr = sqlCmd.ExecuteReader();
   if(dr.Read())
   { 
    return true;
   }
   else
   {
    return false;
   }
  }

  public void VoteFinal()
  {
   //投票数据更新
   bool blnResult; 
   CVote _CVote = new CVote();
   blnResult = _CVote.AddVoteFinal(Convert.ToInt32(this.VoteId), Request.Url.ToString().ToLower(), Request.UrlReferrer.ToString().ToLower(), Request.UserHostAddress, Request.UserAgent, this.UserNameValue, this.MobileValue);
   if(blnResult)
   {     
    //写Cookies
    HttpCookie vote = new HttpCookie("vote");
    DateTime now = DateTime.Now;
    if(Request.Cookies["vote"] != null)
    {
     vote.Value = Request.Cookies["vote"].Value + this.VoteId+ ",";
    }
    else
    {
     vote.Value = this.VoteId + ",";
    }
    vote.Expires = now.AddMinutes(10);
    Response.Cookies.Add(vote);

    Response.Write("<script>alert('您已投票成功!'); window.open('http://www.yocity.cn/blog/'); window.location.href='VoteList_final.aspx';</script>");
   }
  }

  private void btnSumbitVote_Click(object sender, System.EventArgs e)
  {
   this.UserNameValue= Request["txtuserName"]+"";
   this.MobileValue=  Request["txtmobile"]+"";
   this.PassWordValue= Request["txtpassword"]+"";
   this.CheckCodeValue= Request["txtCheckCode"]+"";    
   string errStr;
   if(this.ValidateInput(out errStr))
   {
    this.VoteFinal();
   }
   else
   {
    Response.Write(@"<script>alert('"+ errStr +"');history.back();</script>");
   }  
  }