日期:2014-05-17 浏览次数:20524 次
public partial class Login : System.Web.UI.Page { private static string sValidator = ""; private readonly string sValitorImageUrl = "ValidateImage.aspx?Validator="; private StringBuilder LetterList = new StringBuilder();//可变字符串,用于生成验证码 protected void Page_Load(object sender, EventArgs e) { lab_RegistID.Text = Session["RegistID"].ToString(); if (!Page.IsPostBack) { sValidator = CreatValitedateString(6); ValidateImageLo.ImageUrl = sValitorImageUrl + sValidator; compValidator = sValidator; } } private int GetRandomint(int min, int max)//获取基础字符串的随机数位 { Random random = new Random(); return (random.Next(min, max)); } private void GetLeterlist()//创建验证码的基础字符串 { for (int i = 0; i < 10; i++) { LetterList.Append(i.ToString()); } for (int i = 0; i < 26; i++) { LetterList.Append(((char)(i + (int)'a')).ToString()); } for (int i = 0; i < 26; i++) { LetterList.Append(((char)(i + (int)'A')).ToString()); } } private string CreatValitedateString(int n) { GetLeterlist(); StringBuilder vs = new StringBuilder(); for (int i = 0; i < n; i++) { int index = GetRandomint(0, LetterList.Length - 1); vs.Append(LetterList[index].ToString()); LetterList.Remove(index, 1); } return (vs.ToString()); } protected void btn_Login_Click(object sender, EventArgs e) { if (txt_VlidateNum.Text != sValidator) { lab_ValidateMsg.Text = "请输入正确的验证码!"; return; } else { Response.Redirect("~/HomePage.aspx"); } } }
public string Text { get { return (string)ViewState["Text"]; } set { ViewState["Text"] = value; } }
------解决方案--------------------
我用一个LinkBtton想来执行刷新验证码的
但是clik事件没发生前,RequiredFieldValidator就提示验证码不能为空了
应该怎么做?才能绕开?
=====
其实不用linkbutton来刷新 用啊
function DianJi()//刷新验证码
{ document.getElementById("yan").src = "../include/ValidateCode.aspx" + "?" + Math.random(); }
<img id="yan" alt="刷新验证码" style=" cursor:pointer" onclick="javascript:DianJi();" src="../include/ValidateCode.aspx" />
------解决方案--------------------
验证码的生成是在ValidateImage.aspx页面里的。在其他页面的无效