欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

防止在服務(wù)器處理完成之前用戶多次點擊提交按鈕處理代碼

 更新時間:2012年12月23日 15:24:57   作者:  
在提交表單時,如果網(wǎng)頁速度過慢或者其他原因,用戶多次提交能導致數(shù)據(jù)的修改,怎么解決這個問題呢,接下來將為您解決這個問題,需要的朋友可以了解下

如果網(wǎng)頁速度過慢或者其他原因,用戶多次提交能導致數(shù)據(jù)的修改,怎么解決這個問題呢?
這段是放在 Page_Load 中

復制代碼 代碼如下:

if(!Page.IsPostBack)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append("a();");
s.Append(this.GetPostBackEventReference(this.Button1));
this.Button1.Attributes.Add("onclick",s.ToString());
}
a() 是 JS
function a()
{
var ok=document.getElementById('Button1');
ok.disabled = true;
return true;
}

濃縮后即為
復制代碼 代碼如下:

btnSave.Attributes.Add("onclick","this.disabled='true';"+GetPostBackEventReference(btnSave));

一個問題稍微困擾了一下,后來解決了,btnSave.Attributes.Add("onclick","a();"+GetPostBackEventReference(btnSave)); 如果a()這個函數(shù)還包含其他驗證,比如說一些正則驗證等,btnSave.Attributes.Add("onclick","return a();"+GetPostBackEventReference(btnSave)); 則不能進行??梢詫S代碼全部在CS文件中寫就OK拉。
復制代碼 代碼如下:

System.Text.StringBuilder s = new System.Text.StringBuilder(); s.Append("var ok=document.getElementById('Button1'); ");
s.Append("ok.disabled = true; ");
s.Append(this.GetPostBackEventReference(this.Button1));
this.Button1.Attributes.Add("onclick",s.ToString());
//.net 2.0以上
Button1.Attributes.Add("onclick", "this.disabled=true;" + this.ClientScript.GetPostBackEventReference(Button1, ""));


復制代碼 代碼如下:

<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" OnClientClick="this.value='Sumbit';this.disabled=true; " Text="Sumbit" OnClick="btnSumbit_Click" />

其他的方法(可供嘗試)
方法一
復制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
btn.Attributes.Add("onclick", "state=true;");
StringBuilder sb = new StringBuilder();
sb.Append("if (!state) return;");
sb.Append("var button=document.getElementByIdx_x('btn');");
sb.Append("button.value='Please Wait...';");
sb.Append("document.body.style.cursor='wait';");
sb.Append("button.disabled=true;");
string strScript = "<script>";
strScript = strScript + "var state=false;";
//將函數(shù)綁定到頁面的onbeforeunload事件:
strScript = strScript + "window.attachEvent('onbeforeunload',function(){" + sb.ToString() + "});";
strScript = strScript + "</" + "script>";
Page.RegisterStartupScript("onbeforeunload", strScript);
}
protected void Submit_Click(object sender, EventArgs e)
{
//模擬長時間的按鈕處理
System.Threading.Thread.Sleep(2000);
Response.Write("<script>alert('bbbbbb!!');" + "</" + "script>");
}
<asp:Button ID="btn" Text="Submit" OnClick="Submit_Click"
runat="server"/>

方法2
復制代碼 代碼如下:

<asp:button id="btnSubmit" OnClick="Submit_Click" runat="server" OnClientClick="this.disabled=true;this.form.submit();" UseSubmitBehavior="False"/>

方法3
復制代碼 代碼如下:

this.btnSubmit.Attributes["onclick"]=this.GetPostBackEventReference(this.btnSubmit)+";this.disabled=true;";//防止重復提交

您可能感興趣的文章:

相關(guān)文章

最新評論