2011-11-23 27 views
2

我有一個綁定到ClientScript.RegisterOnSubmitStatement的函數,它在UpdatePanel正在更新時顯示一個JQuery UI對話框(它需要一段時間來更新)。頁面上有2個按鈕,我想根據點擊哪個按鈕在對話框中顯示不同的文本。有沒有辦法做這樣的事情:找出哪個按鈕正在提交頁面的方法?

服務器端

ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", "ShowSplashScreen(this)"); 

客戶方

function ShowSplashScreen(source) { 
// Do stuff depending on the button that was clicked 
} 

目前, 「源」 是的DOM窗口,而不是按鈕。

+0

你可以隨時使用'hidden' HTML場包含按鈕id whi你可以在表格提交後確定。 – ServAce85

+0

我在表單提交之前需要它,因爲我希望啓動屏幕中的文本根據點擊的按鈕而改變。它需要在客戶端驗證後發生在客戶端(這就是爲什麼它需要RegisterOnSubmitStatement),但在表單實際提交之前,因爲我需要在asyc回發期間顯示模態啓動畫面。 – user1004944

回答

3

可以使用__EVENTTARGET發現啓動回發的控制:

/// <summary> 
/// Retrieves the control that caused the postback. 
/// </summary> 
/// <param name="page"></param> 
/// <returns></returns> 
private Control GetControlThatCausedPostBack() 
{ 
    Control ctrl = null; 

    //use the event target to get the control that initiated the postback 
    string ctrlName = Page.Request.Params.Get("__EVENTTARGET"); 
    if (!String.IsNullOrEmpty(ctrlName)) 
     ctrl = Page.FindControl(ctrlName); 

    //return the control to the calling method 
    return ctrl; 
} 
+0

+1這真棒,從來不知道你可以像那樣使用 –

+0

@rick schott:這是一個漂亮的小技巧,當我試圖在回發後保留動態控件的標籤位置時,我立刻回來了:) –

+0

我需要這個客戶端,而不是服務器端 – user1004944

0

OK,我想出了一個解決方法。我首先要做一個簡短的解釋,然後對於那些將來可能會看到這個頁面的人來說更長一段時間,因爲像我這樣的可能不知道這些東西的愛好者可以使用它來獲得一些幫助。 (因爲我生活在谷歌搜索這東西。)

我正在尋找的功能,是具有不同的innerHTML取決於按鈕被點擊的模態div。如果頁面有效,我也只想顯示div,並且並非頁面上的所有按鈕都會導致驗證。

鍛鍊是爲了創建一個全局變量「ButtonClicked」。然後,頁面上的每個按鈕都必須將JavaScript分配給它的onclick屬性,該屬性將ButtonClicked變量設置爲該按鈕的ID。分配給onclick的腳本在BEFORE頁面驗證之前運行。然後,使用ClientScript.RegisterOnSubmitStatement,我分配了一個函數,在成功驗證頁面之後調用,但在實際提交頁面之前。然後,我可以訪問「ButtonClicked」事件以查看剛剛調用了哪個按鈕,然後更改模態div的innerHTML,然後顯示它。 (然後做一個asyc回發,然後在回發後刪除模態div。)

爲什麼我不能直接從按鈕自己調用的函數設置模態div的innerHTML?因爲我放入的innerHTML取決於頁面是否有效,並且我只知道在獲得ShowSplashScreen函數時該頁面是否有效。您也可能會問,爲什麼我不只是調用頁面來驗證從按鈕調用的JavaScript函數。這是因爲這樣做會導致驗證被調用兩次,並且頁面上有很多信息,因此驗證頁面需要幾乎整整一秒的時間,並且由於客戶端驗證函數本身包含一些只能調用一次的內容在驗證期間。

所以最終的結果是function1和function2都是在BEFORE驗證時單擊它們各自的按鈕來調用的,ShowSplashScreen是在驗證後通過任一按鈕調用的(僅當頁面有效時),然後I訪問全局變量以查看哪個被點擊。

所以整個事情是這樣的:

HTML

<!-- This is a simplified version of the HTML that <asp:Button> is going to 
    output in the actual HTML --> 
<input type="submit" id="Button1" value="Load Page" onclick="function1(this)"> 
<input type="submit" id="Button2" value="Save Page" onclick="function2(this)"> 

的JavaScript

var ButtonClicked = ""; //Needs to be global 

//It is not necessary to have a different function for each button, 
//I just needed to for the needs of my page. If Button2 calls function1 
//instead of function2, ButtonClicked is still set to "Button2". 
//It is very important that EVERY button on the page calls something 
//that sets ButtonClicked or you will get an bug if the user ever 
//clicks a button that sets ButtonClicked, and then clicks a button 
//that does not set ButtonClicked (the final function will still 
//think that the first button had just been clicked) 

function function1(source){ 
    ButtonClicked = source.id; 
    //whatever else Client Script that needs to be run from this button 
} 

function function2(source){ 
    ButtonClicked = source.id; 
    //whatever else Clinet Script that needs to be run from this button 
} 

function ShowSplashScreen(){ 
    if(ButtonClicked == "Button1") 
     //Use JQuery to access the dialog <div> and set the innerHTML 
     //to whatever 
    else if(ButtonClicked == "Button2") 
     //Use JQuery to access the dialog <div> and set the innerHTML 
     //to something else 
} 

服務器端

//Use this code to set a function to be called after the page has 
//been successfully validated. If a button does not cause validation, 
//then that button will always call the function set here 
// 
//You should also check to see if the script has already been registered 
//for speed purposes, but I'm just demonstrating particular 
//functionality here. 
protected void Page_Load(object sender, EventArgs e) 
{ 
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", 
              "ShowSplashScreen()"); 
}