如果網(wǎng)頁(yè)速度過(guò)慢或者其他原因,用戶多次提交能導(dǎo)致數(shù)據(jù)的修改,怎么解決這個(gè)問(wèn)題呢?
這段是放在 Page_Load 中
復(fù)制代碼 代碼如下:
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;
}
濃縮后即為:
復(fù)制代碼 代碼如下:
btnSave.Attributes.Add("onclick","this.disabled='true';"+GetPostBackEventReference(btnSave));
一個(gè)問(wèn)題稍微困擾了一下,后來(lái)解決了,btnSave.Attributes.Add("onclick","a();"+GetPostBackEventReference(btnSave)); 如果a()這個(gè)函數(shù)還包含其他驗(yàn)證,比如說(shuō)一些正則驗(yàn)證等,btnSave.Attributes.Add("onclick","return a();"+GetPostBackEventReference(btnSave)); 則不能進(jìn)行。可以將JS代碼全部在CS文件中寫就OK拉。
復(fù)制代碼 代碼如下:
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, ""));
或:
復(fù)制代碼 代碼如下:
asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" OnClientClick="this.value='Sumbit';this.disabled=true; " Text="Sumbit" OnClick="btnSumbit_Click" />
其他的方法(可供嘗試)
方法一:
復(fù)制代碼 代碼如下:
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ù)綁定到頁(yè)面的onbeforeunload事件:
strScript = strScript + "window.attachEvent('onbeforeunload',function(){" + sb.ToString() + "});";
strScript = strScript + "/" + "script>";
Page.RegisterStartupScript("onbeforeunload", strScript);
}
protected void Submit_Click(object sender, EventArgs e)
{
//模擬長(zhǎng)時(shí)間的按鈕處理
System.Threading.Thread.Sleep(2000);
Response.Write("script>alert('bbbbbb!!');" + "/" + "script>");
}
asp:Button ID="btn" Text="Submit" OnClick="Submit_Click"
runat="server"/>
方法2:
復(fù)制代碼 代碼如下:
asp:button id="btnSubmit" OnClick="Submit_Click" runat="server" OnClientClick="this.disabled=true;this.form.submit();" UseSubmitBehavior="False"/>
方法3:
復(fù)制代碼 代碼如下:
this.btnSubmit.Attributes["onclick"]=this.GetPostBackEventReference(this.btnSubmit)+";this.disabled=true;";//防止重復(fù)提交