2011-06-21 84 views
0

我有一個4.0網頁,點擊一下按鈕即可運行服務器端進程,運行時間大約需要5-10秒。我認爲我會很聰明,並搜索一些代碼,使該按鈕隱藏,並在進程運行時顯示動畫gif圖像。
我得到了這個工作,但開始注意到「被另一個進程使用的文件」錯誤被拋出服務器端(部分進程正在刪除一個目錄)。我調試通過並開始看到什麼似乎是第二個按鈕事件點擊同時被解僱。事實證明,我添加的一段Js代碼來刷新頁面以允許運行GIF動畫,似乎是第二次觸發服務器點擊事件。我不得不調用Js SetTimeOut()方法來運行gif圖像,這就是看起來會導致問題的原因。
我做了一些搜索嘗試找到一種不同的方法來讓動畫gif運行,但找不到一個,所以我去了懸崖方法底部的救護車,並決定添加一個服務器端時間戳記到會話中,並且只有在自上次設置會話以來超過20秒時才繼續執行我的代碼。
我已經有一些非常不穩定的結果,這種方法和任何輸入將不勝感激。.net 4.0網頁按鈕點擊事件觸發兩次

  1. 有沒有更好的方法可以讓gif運行?
  2. 有沒有其他人遇到這個事件發生兩次?
  3. 爲什麼我的鎖不能第一次通過?

任何建議更好的方法來做到這一點表示讚賞,謝謝!

記錄的結果:

通過(沒有會話現有)

第一次

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> 
+1

順便說一句,你不能只調用一個變量'鎖定',並讓它與多個線程一起工作。 –

+0

是的,同意,這就是爲什麼我設置變量鎖定基於會話變量「ClickTime」應該跨越不同的線程。 – Ian

+0

我會說,你沒有綁定事件兩次..在aspx和代碼背後。 –

回答

0

我認爲它更好地利用一些網絡調試工具來跟蹤請求。我建議使用Fiddler

0

我懷疑這個問題可能與動畫gif沒有關係,但按鈕被按下兩次(即兩次點擊獲取註冊,而不是一個)。您可以嘗試禁用按鈕剿第二次點擊(另一種方法是在按鈕水平有標誌) - 例如,

btnGo.OnClientClick = "return HideControlOnClick(this);"; 


function HideControlOnClick(btnGo) 
{ 
    if (btnGo["My_Is_Clicked"]) { 
     // already clicked, ignore 
     return false; 
    } 
    btnGo["My_Is_Clicked"] = true; 
    ... 
    return true; 
} 
+0

我不認爲這是客戶端雙擊,因爲一旦我刪除setTimeout(「UpdateImg('MainContent_imgWait','Images/loading.gif');」,50);來自Js塊的方法調用阻止重複呼叫,但gif圖像不會運行。 – Ian

0

我已經通過刪除所有相關的控制到一個更新解決的問題pannel,這似乎允許動畫gif在不調用setTimeout的情況下運行(「UpdateImg('MainContent_imgWait','Images/loading.gif');」,50); JS方法。現在正在使用服務器事件,只發射一次。

0

這個問題很可能是你使用服務器端的按鈕控件(因爲你引用它的服務器端),並且當它們被點擊時通常會觸發回發(很難說不知道確切的對象你正在使用)。使用HTML輸入來激發你的客戶端事件,完成你的工作,然後從你的JS代碼執行回發(使用適當的ID在服務器端捕獲)。

事情是這樣的服務器端:

/// <summary> 
/// Page init event, setup the core of the page. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
protected override void Page_Init(object sender, EventArgs e) 
{ 
    try 
    { 
    // See if we're in a postback. 
    if (IsPostBack) 
    { 
     // If we have a target... 
     if (Request.Params["__EVENTTARGET"] != null) 
     { 
     // See what the target is. 
     switch (Request.Params["__EVENTTARGET"]) 
     { 
      case "btnGo": 
      // Maybe make this a parameterless method rather than an event handler to avoid parameters that you don't need. 
      btnGo_Click(null, null); 
      break; 
     } 
     } 
    } 
    } 
} 

和客戶端:

function Go() { 
    // Show loading... 

    // Call server. 
    __doPostBack("btnGo", "My Args"); 
    } 

編輯:作爲替代我想你也可以添加 「返回false;」在客戶端事件後,如:

btnGo.OnClientClick =「HideControlOnClick(this); return false;」;

並且應該停止回發。

相關問題