首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

|G|禁止按钮点2次的有关问题!在网速很慢时,误点两次,怎么处理

2011-12-14 
|G|禁止按钮点2次的问题!在网速很慢时,误点两次,怎么处理?希望帮帮忙!因为有些只能点击一次!我做了个简单

|G|禁止按钮点2次的问题!在网速很慢时,误点两次,怎么处理?
希望帮帮忙!
因为有些只能点击一次!我做了个简单实例!
public   partial   class   _Default   :   System.Web.UI.Page  
{
        public   static     int   i   =   10;
        public   static     int   j   =   10;
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {

        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                i--;
                Response.Write(i.ToString());
        }
        protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                Button2.Enabled   =   false;
                i--;
                Response.Write(i.ToString());
        }
}
-----------
-----------
1.但是调试的时候,无论我多么快的点2次,click只执行一次!
2.Button2的这种写法,能不能保证无论网速多么慢,按钮不会点击2次!

[解决办法]
使用button按钮 不要 使用 服务器按钮和submit按钮

<input id= "Button1 " type= "button " onclick= "this.disabled=true; " value= "提交 " onserverclick= "Button1_ServerClick " runat= "server " />


.cs 执行完你的操作后

Page.RegisterStartupScript( "enBtn ", " <script> document.getElementById( 'Button1 ').disabled=false; </script> ");
[解决办法]
if (!this.IsPostBack) { Session[ "CanClick "] = true; } protected void Button1_Click(object sender, EventArgs e) { if ((bool)Session[ "CanClick "]) { //do something Session[ "CanClick "] = false; } else { Response.Write( "不可多次点击! "); } }
[解决办法]
SESSION记录点的次数
[解决办法]
在客户端做,点击按钮后用javascript把按钮状态设为不可用就可以
<asp:Button ID= "Button1 " runat= "server " Text= "Button " OnClientClick= "this.disabled=true;return true; " OnClick= "Button1_Click " />
========
说这种话的人不负责任,自己根本没试过,LZ,给你一份自定义控件的代码,自己编译成控件就可以实现了
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
namespace gunion.Utility
{

/**/
/// <summary>
/// 显示BUTTON控件,让控件只能按一次
/// </summary>
public class OneClickButton : System.Web.UI.WebControls.Button
{
private string waitText = "请稍等正在提交 ";
private string warningText = "确定保存吗?(Yes/No) ";
private bool _ShowMessageBox = true;
private Color BgColor;
private CurSor _Cursor = CurSor.auto;

public enum CurSor
{
hand = 0,
text = 1,
wait = 2,


help = 3,
auto = 4,
}

/**/
/// <summary>
/// 获取或设置按纽按下后文本
/// </summary>
[DefaultValue( "请稍等 ")]
[Description( "这个文本在按纽按下后显示 ")]
[Category( "Appearance ")]
public string WaitText
{
get
{
return this.waitText;
}
set
{
this.waitText = value;
}
}

/**/
/// <summary>
///获取或设置警告字符串
/// <summary>
[DefaultValue( "确定保存吗?(Yes/No) ")]
[Description( "警告框文本 ")]
[Category( "Appearance ")]
public string WarningText
{
get
{
return this.warningText;
}
set
{
this.warningText = value;
}
}

/**/
/// <summary>
///设置是否显示确认对话框
/// </summary>
[DescriptionAttribute( "是否显示对话框 ")]
[DefaultValueAttribute( "false ")]
[Category( "Appearance ")]
public bool ShowMessageBox
{
get
{
return this._ShowMessageBox;
}
set
{
this._ShowMessageBox = value;
}
}
/**/
/// <summary>
///设置按纽按下后背景颜色
/// </summary>
[DescriptionAttribute( "设置按纽背景颜色 ")]
[DefaultValue( " ")]
[Category( "Appearance ")]
public Color SetBackGroundColor
{
get
{
return this.BgColor;
}
set
{
this.BgColor = value;
}
}


/**/
/// <summary>
/// 获取或设置按纽按下后鼠标形状
/// </summary>
[DefaultValue( " ")]
[Description( "鼠标形状 ")]
[Category( "Appearance ")]
public CurSor SetCurSor
{
get
{
return this._Cursor;
}
set
{
this._Cursor = value;
}
}
/**/
/// <summary>
///控件呈显前注册客户端脚本
/// </summary>
/// <param name= "e "> </param>
protected override void OnPreRender(EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (this._ShowMessageBox)
{
sb.AppendFormat( "if(!confirm( '{0} ')) ", this.WarningText);
sb.Append( "{return false;} ");
}
sb.Append( "if (typeof(Page_ClientValidate) == 'function ') { ");
sb.Append( "if (Page_ClientValidate() == false) { return false; }} ");
sb.AppendFormat( "this.value = '{0} '; ", this.waitText);
sb.Append( "this.disabled = true; ");
sb.AppendFormat( "document.body.style.cursor= '{0} '; ", SetCurSor);


sb.AppendFormat( "this.style.backgroundColor= '{0} '; ", ColorTranslator.ToHtml(this.SetBackGroundColor));
sb.Append(this.Page.GetPostBackEventReference(this));
sb.Append( "; ");
this.Attributes[ "onclick "] = sb.ToString();
base.OnPreRender(e);
}
}

public class OneClickImageButton : System.Web.UI.WebControls.ImageButton
{
private string waitText = "请稍等正在提交 ";
private string warningText = "确定保存吗?(Yes/No) ";
private bool _ShowMessageBox = true;
private Color BgColor;
private CurSor _Cursor = CurSor.auto;

public enum CurSor
{
hand = 0,
text = 1,
wait = 2,
help = 3,
auto = 4,
}

/**/
/// <summary>
/// 获取或设置按纽按下后文本
/// </summary>
[DefaultValue( "请稍等 ")]
[Description( "这个文本在按纽按下后显示 ")]
[Category( "Appearance ")]
public string WaitText
{
get
{
return this.waitText;
}
set
{
this.waitText = value;
}
}


[解决办法]
1.disable button,然后用ajax 或者用xmlhttp来发送请求;

2.用个hidden的input,每次刷新页面生成一个guid,回传时服务端判断guid是否有效;

热点排行