2013-02-27 64 views
0

有這樣的代碼:在ASP防止重複submiting:CommandField中

<asp:UpdatePanel ID="id"> 
<asp:CommandField ButtonType="Image" InsertImageUrl="url/here" ShowInsertImage="true" ValidationGroup="valGr" /> 
</asp:UpdatePanel> 

如何防止這個按鈕在C#代碼雙擊。

+0

也許有些JavaScript會在第一次點擊的X毫秒內發生任何點擊事件? – 2013-02-27 18:41:30

+0

是的,你是對的,我已經找到了一些JavaScript代碼,但我不知道如何找到這個按鈕,因爲我怎麼知道asp自動生成html代碼,它使我很難找到這個按鈕。 – 2013-02-27 18:44:15

+0

是的自動生成的ID是一個痛苦。如果你發佈的JavaScript我可以告訴你如何做的元素選擇 – 2013-02-27 18:56:02

回答

0

您可以先創建一個標誌來保存提交併將其添加到表單中。然後在UpdatePanel進行更新並等待返回時打開並關閉該標誌。

例如這個代碼是允許提交表單只有當 fAlloToSubmittrue(和在UpdatePanel)的:

<script> 
    var fAlloToSubmit = true; 

    function AllowFormToRun() 
    { 
     if(!fAlloToSubmit) 
      alert("Please wait for the page to fully re-loaded."); 

     return fAlloToSubmit; 
    } 
</script> 

和你後面的代碼設置表單上。

protected void Page_Load(object sender, EventArgs e) 
{ 
    Page.Form.Attributes["onsubmit"] = "return AllowFormToRun();"; 
} 

現在的UpdatePanel的,你打開國旗,你去更新的時刻,例如時刻,你點擊命令:

<script> 
var prm = Sys.WebForms.PageRequestManager.getInstance();  
prm.add_initializeRequest(InitializeRequest); 
prm.add_endRequest(EndRequest); 

function InitializeRequest(sender, args) { 
    // here is run when you click the command on the UpdatePanel  
    fAlloToSubmit = false; 
} 

function EndRequest(sender, args) { 
    // here is come after the upadate of the command 
    fAlloToSubmit = true; 
} 
</script> 

這裏的竅門是,我認爲形式提交而不是查看頁面上的每個控件以禁用它。

+0

我會嘗試你的代碼,我會給出反饋。 – 2013-02-27 19:23:49

+0

@MorarMihai This – Aristos 2013-02-27 20:01:30

+0

你的意思是說,Page.Form.Attributes [「onsubmit」]接受了提交的按鈕,即使可能有更多的按鈕(例如InsertButton,EditButton,CancelButton等等)。所以,如果你喜歡它,你可以修改它或改進它。該代碼必須適用於所有類型的按鈕?腳本我可以把它放在同一個文件中? – 2013-02-28 06:52:40