我有一個4.0網頁,點擊一下按鈕即可運行服務器端進程,運行時間大約需要5-10秒。我認爲我會很聰明,並搜索一些代碼,使該按鈕隱藏,並在進程運行時顯示動畫gif圖像。
我得到了這個工作,但開始注意到「被另一個進程使用的文件」錯誤被拋出服務器端(部分進程正在刪除一個目錄)。我調試通過並開始看到什麼似乎是第二個按鈕事件點擊同時被解僱。事實證明,我添加的一段Js代碼來刷新頁面以允許運行GIF動畫,似乎是第二次觸發服務器點擊事件。我不得不調用Js SetTimeOut()方法來運行gif圖像,這就是看起來會導致問題的原因。
我做了一些搜索嘗試找到一種不同的方法來讓動畫gif運行,但找不到一個,所以我去了懸崖方法底部的救護車,並決定添加一個服務器端時間戳記到會話中,並且只有在自上次設置會話以來超過20秒時才繼續執行我的代碼。
我已經有一些非常不穩定的結果,這種方法和任何輸入將不勝感激。.net 4.0網頁按鈕點擊事件觸發兩次
- 有沒有更好的方法可以讓gif運行?
- 有沒有其他人遇到這個事件發生兩次?
- 爲什麼我的鎖不能第一次通過?
任何建議更好的方法來做到這一點表示讚賞,謝謝!
記錄的結果:
通過(沒有會話現有)第一次
2011-06-21 11:46:14.8968 | DEBUG | FileViewer.copyfiles |計數= 1 &鎖定=假
2011 -06-21 11:46:14.8968 | DEBUG | FileViewer.copyfiles | Count = 2 & Locked = False
2011-06-21 11:46:19.0619 | DEBUG | FileViewer.copyfiles | Count = 1 & Locked = False
2011-06-21 11:46:19.0619 | DEBUG | FileViewer.copyfiles | Count = 2 &鎖定=真
2011-06-21 11:46:23.1959 | DEBUG | FileViewer.copyfiles |計數= 3 &鎖定=真
2011-06-21 11:46:28.8119 | DEBUG | FileViewer.copyfiles |計數= 3 &鎖定=真
再次執行命令
2011-06-21 11:49:47.7798 | DEBUG | FileViewer.copyfiles |計數= 1 &鎖定=假
2011-06- 21 11:49:47.7798 | DEBUG | FileViewer.copyfiles | Count = 2 &鎖定= True
2011-06-21 11:49:55.9697 | DEBUG | FileViewer.copyfiles | Count = 3 & Locked = True
2011-06-21 11:49:59.8697 | DEBUG | FileViewer.copyfiles | Count = 1 & Locked = True
2011-06-21 11:49:59.8697 | DEBUG | FileViewer。copyfiles |計數= 3 &鎖定=真
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PostBackOptions optionsSubmit = new PostBackOptions(btnGo);
btnGo.OnClientClick = "HideControlOnClick(this);";
btnGo.OnClientClick += ClientScript.GetPostBackEventReference(optionsSubmit);
}
}
protected void btnGo_Click(object sender, EventArgs e)
{
bool locked = true;
if (Session["ClickTime"] == null || (DateTime)Session["ClickTime"] < DateTime.Now.AddSeconds(-20))
{
Session["ClickTime"] = DateTime.Now;
locked = false;
}
WriteToLog(1, locked);
if (Page.IsValid && !locked)
{
locked = true;
WriteToLog(2, locked);
// Do all my processing
}
WriteToLog(3, locked);
}
<script language="javascript" type="text/javascript">
function HideControlOnClick(btnGo)
{
// IE uses className for the css property.
btnGo.setAttribute('className', 'hide');
document.getElementById('MainContent_imgWait').setAttribute('className', 'show');
setTimeout("UpdateImg('MainContent_imgWait','Images/loading.gif');",50);
}
function UpdateImg(ctrl, imgsrc)
{
var img = document.getElementById(ctrl);
img.src = imgsrc;
}
</script>
順便說一句,你不能只調用一個變量'鎖定',並讓它與多個線程一起工作。 –
是的,同意,這就是爲什麼我設置變量鎖定基於會話變量「ClickTime」應該跨越不同的線程。 – Ian
我會說,你沒有綁定事件兩次..在aspx和代碼背後。 –