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()");
}
你可以隨時使用'hidden' HTML場包含按鈕id whi你可以在表格提交後確定。 – ServAce85
我在表單提交之前需要它,因爲我希望啓動屏幕中的文本根據點擊的按鈕而改變。它需要在客戶端驗證後發生在客戶端(這就是爲什麼它需要RegisterOnSubmitStatement),但在表單實際提交之前,因爲我需要在asyc回發期間顯示模態啓動畫面。 – user1004944