一个页面,需要验证码,我自己写了一个验证码
     在提交数据的时候,问题就来了
     如果我验证码输入是错误的,他提示验证码输入不正确!
     然后我在刷新页面,他又会在一次的去验证,结果又提示 验证码输入不正确
     现在我想 但验证码有误,提示之后,在刷新页面,页面不会再次验证...     提示代码是这样的  
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('验证通过!');</script>", false);

解决方案 »

  1.   


    刚刚用 if(!IsPostBack)  和 if(IsPostBack == false) 都试了试,结果都是老样子...
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;/// <summary>
    /// BasePage 的摘要说明
    /// </summary>
    public class BasePage:System.Web.UI.Page
    {
    public BasePage()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
        }
        #region IsRefreshed
        public bool IsRefreshed
        {
            get
            {
                string requestPath = HttpContext.Current.Request.Path;
                return (bool)HttpContext.Current.Items[requestPath + "_IsRefreshed"];
            }
        }
        #endregion
        protected override void  OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            string requestPath = HttpContext.Current.Request.Path;
            int ticket = int.Parse(HttpContext.Current.Items[requestPath + "_NextTicket"].ToString());
            ClientScript.RegisterHiddenField("CurrentTicket", ticket.ToString());
        }
    }public class NoRepeatOperModel : IHttpModule
    {
        public NoRepeatOperModel()
        {
        }
        static Hashtable historyRequest = null;
        private string RequestPath
        {
            get
            {
                return HttpContext.Current.Request.Path;
            }
        }
        #region IHttpModel 成员
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            if (historyRequest == null)
            {
                historyRequest = new Hashtable();
            }
            context.BeginRequest += delegate(object sender, EventArgs e)
            {
                int lastTicket = GetLastTicket();
                int currentTicket = GetCurrentTicket(lastTicket);
                if (currentTicket > lastTicket || (lastTicket == currentTicket && currentTicket == 0))
                {
                    HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = false;
                    historyRequest[RequestPath] = currentTicket;
                }
                else
                {
                    HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = true;
                }
            };
        }
        #endregion
        private int GetLastTicket()
        {
            if (!historyRequest.ContainsKey(RequestPath))
            {
                return 0;
            }
            return int.Parse(historyRequest[RequestPath].ToString());
        }
        private int GetCurrentTicket(int LastTicket)
        {
            int ticket;
            string currentTicket = HttpContext.Current.Request["CurrentTicket"];
            if (currentTicket == null)
            {
                ticket = LastTicket;
            }
            else
            {
                ticket = int.Parse(currentTicket);
            }
            //保存下一个标号
            HttpContext.Current.Items[RequestPath + "_NextTicket"] = ticket + 1;
            return ticket + 1;
        }
    }
    让你的页面继承自basepage,代码部分用isrefreshed做判断试一下。