zoukankan      html  css  js  c++  java
  • asp.net 防重复提交submit控件

    在页面post提交的时候,我们经常会碰到重复提交的的问题,前面有高手写过防刷新的办法,是基于viewstate,这样就会有一个问题:必须使 用ruant="server"的form,有的时候我们仅需要使用客户端form就可以,防刷新,防重复提交就又成了一个问题。

        下面我采用session和hidden,写了一个submit控件,能够有效的防止重复提交

    代码如下:

    Shenjk.Controls.Submitusing System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    namespace Shenjk.Controls
    {
        /// <summary>
        /// 防止重复提交
        /// http://www.shenjk.com
        /// </summary>
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:Submit runat=server></{0}:Submit>")]
        public class Submit:System.Web.UI.WebControls.WebControl
        {
            private HtmlInputSubmit m_Submit = new HtmlInputSubmit("submit");
            private HtmlInputHidden m_Hidden = new HtmlInputHidden();
            public Submit() {
             
            }
            #region
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Value {
                get { return m_Hidden.Value; }
                set { m_Hidden.Value = value; }
            }
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("发送")]
            [Localizable(true)]
            public string Text {
                get { return m_Submit.Value; }
                set { m_Submit.Value = value; }
            }
            #endregion

            protected override void Render(HtmlTextWriter writer)
            {
                if (!this.Width.IsEmpty)
                {
                    this.m_Submit.Style.Add("width", this.Width.ToString());
                }
                if (!this.Height.IsEmpty) {
                    this.m_Submit.Style.Add("height", this.Height.ToString());
                }
                if (!string.IsNullOrEmpty(this.CssClass)) {
                    this.m_Submit.Attributes.Add("class", this.CssClass.ToString());
                }

                this.m_Hidden.ID = this.ClientID;
                this.m_Hidden.Value = System.DateTime.Now.ToString();
                this.m_Hidden.RenderControl(writer);
                this.m_Submit.ID = this.ClientID + "_Submit";
                this.m_Submit.Attributes.Add("onclick","this.style.border='1px solid #f00';this.value='正在发送...';this.style.backgroundColor='#fff'; /*this.disabled=true;return true;*/");
                this.m_Submit.RenderControl(writer);
               
            }

            public bool IsRefresh() {
                if (System.Web.HttpContext.Current.Session[this.ClientID]==null || (System.Web.HttpContext.Current.Session[this.ClientID] != null && System.Web.HttpContext.Current.Request.Form[this.ClientID] != System.Web.HttpContext.Current.Session[this.ClientID].ToString()))
                {
                    return false;
                }
                return true;
            }
            public void ReSet() {
                if (System.Web.HttpContext.Current.Session[this.ClientID] != null) {
                    System.Web.HttpContext.Current.Session.Remove(this.ClientID);
                }
            }
            protected override void OnLoad(EventArgs e)
            {
                if (System.Web.HttpContext.Current.Request.HttpMethod == "POST") {
                    System.Web.HttpContext.Current.Session[this.ClientID] = System.Web.HttpContext.Current.Request.Form[this.ClientID].ToString();
                }
                base.OnLoad(e);
            }
        }
    }

    使用示例:

    test.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
    <%@ Register assembly="Shenjk.Controls" namespace="Shenjk.Controls" tagprefix="cc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
      
        <form id="form1" runat="server">
       <asp:Label ID="Label1" runat="server"></asp:Label><br />
       <asp:Label ID="Label2" runat="server"></asp:Label><br />
        <cc1:Submit ID="Submit1" Text="发送" Width="100" Height="50" runat="server" />
        </form>
    </body>
    </html>

    test.aspx.cs

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.HttpMethod == "POST")
            {
                if (Submit1.IsRefresh()) {
                    Label1.Text = "请不要直接刷新页面,且不要重复提交";
                }

            }
        }
    }

  • 相关阅读:
    Window 窗口类
    使用 Bolt 实现 GridView 表格控件
    lua的table库
    Windows编程总结之 DLL
    lua 打印 table 拷贝table
    使用 xlue 实现简单 listbox 控件
    使用 xlue 实现 tips
    extern “C”
    COleVariant如何转换为int double string cstring
    原来WIN32 API也有GetOpenFileName函数
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1383143.html
Copyright © 2011-2022 走看看