0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【ASP.NET】コードビハインドで同期/非同期ポストバックによる分岐を実装する

Last updated at Posted at 2019-04-09

マスターページの利用を前提としています。

非同期ポストバックを利用するためには、ScriptManagerToolkitScriptManagerのどちらかをマスターページに記載しておく必要があります。

hogehoge.master
<!-- ここで指定した ID を後ほど利用します -->
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
hugahuga.aspx.vb
' ページロードなどのイベント
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    ' ポストバックそうでないかを判定できる
    If IsPostBack Then
        ' 同期/非同期に関わらずポストバック時に実行される
        ' 画面の再表示などで使われる

    Else
        ' ポストバックではない場合に実行される
        ' 画面の初期表示などで使われる

    End If

    ' こちらは同期ポストバックか非同期ポストバックかを判定できる
    ' hogehoge.masterで指定した ID を使う
    If DirectCast(Master.FindControl("ToolkitScriptManager1"), ScriptManager).IsInAsyncPostBack Then
        ' なんらかの非同期処理

    Else
        ' なんらかの同期処理

    End If

End Sub

また、JavaScript側で非同期通信の開始/終了時に実行したい処理を記述することもできます。

hugahuga.aspx
    addEventListener('load', function () {
        const mng = Sys.WebForms.PageRequestManager.getInstance();

        mng.add_initializeRequest(function (sender, args) {
            // 非同期処理の開始時に実行したいなんらかの処理
            // テキストボックスを一時的に入力不可にしたいときなどに使えます。
        });

        mng.add_endRequest(function (sender, args) {
            // 非同期処理の終了時に実行したいなんらかの処理
        });
    }, false);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?