2

UpdatePanel の更新中に JQuery UI ダイアログを表示する ClientScript.RegisterOnSubmitStatement に関連付けられた関数があります (更新には時間がかかります)。ページには 2 つのボタンがあり、クリックされたボタンに応じてダイアログに異なるテキストを表示したいと考えています。このようなことをする方法はありますか:

サーバ側

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

クライアント側

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

現在、「ソース」はボタンではなく DOM ウィンドウです。

4

2 に答える 2

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; 
} 
于 2011-11-23T18:19:43.090 に答える
0

OK、回避策を見つけました。最初に簡単な説明をしてから、将来このページを見る可能性のある人のために、より長い説明をします. (私はこのようなものをグーグルで生きているからです。)

私が求めていた機能は、クリックされたボタンに応じて異なる innerHTML を持つモーダル div を持つことでした。また、ページが有効で、ページ上のすべてのボタンが検証を引き起こすわけではない場合にのみ div を表示したかったのです。

ワークアウトは、グローバル変数「ButtonClicked」を作成することでした。次に、ページ上のすべてのボタンに、ButtonClicked 変数をボタンの ID に設定する onclick 属性に JavaScript を割り当てる必要があります。onclick に割り当てられたスクリプトは、ページの検証前に実行されます。次に、ClientScript.RegisterOnSubmitStatement を使用して、ページが正常に検証された後、実際にページを送信する直前に呼び出される関数を割り当てました。次に、「ButtonClicked」イベントにアクセスして、呼び出されたボタンを確認し、モーダル div の innerHTML を変更して表示します。(次に、非同期ポストバックを実行し、ポストバック後にモーダル div を削除します。)

ボタンが自分自身を呼び出す関数からモーダル div の innerHTML を設定できないのはなぜですか? そこに入れている innerHTML はページが有効かどうかに依存するため、ShowSplashScreen 関数を取得したときにページが有効かどうかしかわかりません。また、ボタンが呼び出す JavaScript 関数内から検証するページを呼び出さない理由を尋ねることもできます。これは、これを行うと検証が 2 回呼び出され、ページに非常に多くの情報が含まれているため、ページの検証にほぼ 1 秒かかり、クライアント側の検証関数自体に 1 回しか呼び出せないものが含まれているためです。検証中。

したがって、これらすべての最終結果は、検証前にクリックするとそれぞれのボタンによって function1 と function 2 の両方が呼び出され、検証後にいずれかのボタンによって ShowSplashScreen が呼び出され (ページが有効な場合のみ)、グローバル変数にアクセスすることです。どちらがクリックされたかを確認します。

したがって、全体は次のようになります。

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()");
}
于 2011-11-24T04:07:01.407 に答える