1

masterlayout にサーバー側関数を追加すると、すべてのページにポップアップが表示されます。ただし、「いいえ」ボタンをクリックすると、表示されなくなります。そのためにはセッションを使用する必要がありますが、jQuery でセッション値を設定することはできません。

masterlayout で使用するコード ビハインドは次のとおりです。

     <script runat="server">
      protected void btnCancel_Click(object sender, EventArgs e)
      {
        Session["sesvalue"] = 1;

      }
     </script>

しかし、メソッドはボタンのクリックでは起動しません

4

2 に答える 2

3

メソッドには static 属性が必要です。

[WebMethod]
public static string MethodName()
{
   return "";
}
于 2012-09-25T11:31:50.240 に答える
1

jQuery からサーバー側関数を呼び出す方法は、ajax リクエストを使用することです。Session に何も入れる必要はありません。クライアント側から値をパラメーターとしてサーバー側の関数に渡すだけで済みます。次に例を示します。

function ShowDialogAndCallServerSideFunction()
{
    var $dialog = $('<div class="dialog"></div>')
    .html('Dialog content goes here')
    .dialog({
        autoOpen: false,
        width: 320,
        title: 'Title goes here',
        closeOnEscape: true,
        buttons: [
            {
                text: "No",
                click: function() { $(this).dialog("close"); }
            },
            {
                text: "Yes",
                click: function() {

                        $.ajax({
                            "type": "POST",
                            "dataType": 'json',
                            "contentType": "application/json; charset=utf-8",
                            "url": "WebServiceUrl.asmx/MethodName",
                            "data": "{'parameter': " + your_parameterHere + " }",
                            "success": function(result) {
                                //handle success here
                            },
                            "error": function(xhr, ajaxOptions, thrownError) {
                                //handle any errors here
                            }
                        });
                    $(this).dialog("close");
                }
            }
        ]
    });
    $dialog.dialog('open');
}

サーバー側では、Web サービス (私の例では WebServiceUrl と呼ばれます) を持つことができます。

[WebMethod]
public void MethodName(string parameter)
{
   //the value received in 'parameter' is the value passed from the client-side via jQuery
}
于 2012-04-16T14:07:21.487 に答える