按钮做一个用户的帖子

时间:2012-07-09 19:14:35

标签: c# asp.net

我不确定我是否在正确的区域,但我会尝试一下。

我的页面后面有一个带有C代码的asp.net页面,我有一个提交按钮,用于在页面上保存表单。现在我的问题是我有500个用户没有任何问题,1个用户必须点击它5到6次才能触发它。它会每次回发但不会保存。

如果需要,我可以发布一些代码,我已经查看了所有内容,她的记录和计算机与其他人只是寻找想法没有什么不同。

所有,欢迎任何帮助,谢谢大家。

编辑:

所有用户必须使用IE7或更好的

<asp:Button ID="button" runat="server" />

代码背后:

protected override void OnInit(EventArgs e)
{
    button.EnableViewState = true;

    InitializeComponent();
    base.OnInit(e);
}

private void InitializeComponent()
{
    this.button.Click += new EventHandler(this.button_Click);
}

protected void button_Click(object sender, EventArgs e)
{
    if (((Session["value"] == null) || (Session["value"] == "")))
    {
        Response.Redirect("login.aspx");
    }
    else
    {
        checkNoteValues("Inc");
    }
}

1 个答案:

答案 0 :(得分:1)

你可以:

  1. 使用DIV锁定屏幕;
  2. 点击后停用按钮;
  3. 使用“Synchronizer Token”模式;
  4. 选项1和2易受一个(或多个)页面重新加载(F5)的影响。 选项3更加强大,理论上是万无一失的。

    很快我会发布1和3的示例代码。

    编辑:

    Helper类到“Synchronizer Token”

    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;
    
    namespace TestDivLockScreen
    {
        /// <summary>
        /// Helper to "Synchronizer Token Pattern".
        /// </summary>
        public class SynchronizerToken
        {
            private static readonly string SESSION_KEY_TOKEN = "TestDivLockScreen.SynchronizedToken.SESSION_KEY_TOKEN";
    
            /// <summary>
            /// Returns a new token and positions it for validation of next request.
            /// </summary>
            /// <returns></returns>
            public static string NewToken()
            {
                string token = Guid.NewGuid().ToString("N");
                HttpContext.Current.Session.Add(SESSION_KEY_TOKEN, token);
                return token;
            }
    
            /// <summary>
            /// Returns the value of the current token. Renew if the current token is null.
            /// </summary>
            /// <returns></returns>
            public static string CurrentToken()
            {
                string token = HttpContext.Current.Session[SESSION_KEY_TOKEN] as string;
                if (string.IsNullOrEmpty(token))
                {
                    token = NewToken();
                }
    
                return token;
            }
    
            /// <summary>
            /// Checks if the token matches the token of last call to NewToken.
            /// The removal of the token is only made after a new call NewToken.
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            public static bool IsCurrentToken(string token)
            {
                string currentToken = HttpContext.Current.Session[SESSION_KEY_TOKEN] as string;
                if (currentToken == null)
                {
                    return false;
                }
                else
                {
                    if (currentToken.Equals(token))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
    
            /// <summary>
            /// Do the same as IsCurrentToken. However loads a new "token value"
            /// in the session. Is "Thread Safe"!
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            public static bool IsCurrentTokenRenew(string token)
            {
                lock (HttpContext.Current.Session)
                {
                    string currentToken = CurrentToken();
                    NewToken();
                    if (currentToken.Equals(token))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
    }
    

    在页面上定义隐藏字段:

    <asp:HiddenField ID="HdfSyncToken" runat="server" />
    

    代码背后:

    protected void Pre_Render(object sender, EventArgs e)
    {
        //repositioning token
        this.HdfSyncToken.Value = SynchronizerToken.CurrentToken();
    
        //Refresh GridView
        this.GrvRecords.DataSource = this.Records;
        this.GrvRecords.DataBind();
    }
    
    protected void BtnInsertST_Click(object sender, EventArgs e)
    {
        //Abort second execution for the same value of the token
        if (!SynchronizerToken.IsCurrentTokenRenew(this.HdfSyncToken.Value))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script type='text/javascript'>alert('Request has already been answered!');</script>");
        }
        //Insert Record (token validation OK)
        else
        {
            this.InsertRecord();
        }
    }
    

    完全来源(VS2005):q_11402631.7z