0

詳細: HTML コードをページにロードし、<a>リンクをクリックすると、このクリック イベントがキャプチャされ、コード ビハインドによって処理されるようにします。

クライアント側のコードとして私が持っているものは次のとおりです。

$('a').click(function (e) {
        event.preventDefault();
        var data = { userName: $(this).attr("id") };
        var dataVal = JSON.stringify(data);

        $.ajax({
            type: "POST",
            url: "Default.aspx/loadNewPage",
            contentType: "application/json; charset=utf-8",
            data: dataVal,
            dataType: "json",
            success: function (id) {
            }
        });
    });

HTML:

<a href="#" id="kontakt">Go to Kontakt</a>

問題は、コード ビハインド関数が呼び出されないことですが#、URL に追加されるだけです...

コード例とおそらくそれらの説明を使用してコードを修正していただければ幸いです。

更新: ASP ページの webmethods で試しました。分離コード:

[WebMethod]
public string loadNewPage(string id)
{
    ltlContentCenter.Text = getPageCenter("kontakt");
    ltlContentSide.Text = getPageSide("kontakt");
    return "hello";
}

JS コード:

function loadMe() {
        PageMethods.loadNewPage("kontakt", CallSuccess, CallError);
    }
    function CallSuccess(res) {
        alert(res);
    }
    function CallError() {
        alert('Error');
    }

HTML:

    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="scpt" runat="server" EnablePageMethods="true" />
        <div id="parent">
        <div id="mainmenu">
            <asp:LinkButton ID="LinkButton1" Text="FORSIDEN" CommandArgument="forsiden" OnCommand="loadPage_Command"
                runat="server" />
            <br />
            <asp:LinkButton ID="LinkButton2" Text="PRODUKTER" CommandArgument="produkter" OnCommand="loadPage_Command"
                runat="server" />
            <br />
            <asp:LinkButton ID="linkPages" Text="SUPPORT" CommandArgument="media" OnCommand="loadPage_Command"
                runat="server" />
            <br />
            <asp:LinkButton ID="LinkButton3" Text="KONTAKT" CommandArgument="kontakt" OnCommand="loadPage_Command"
                runat="server" />
            <br />
            <asp:LinkButton ID="LinkButton4" Text="OM ABiX" CommandArgument="omabix" OnCommand="loadPage_Command"
                runat="server" />
            <br />
        </div>
        <div id="logo">
        </div>


        <asp:Literal ID="ltlContentCenter" runat="server" />
        <asp:Literal ID="ltlContentSide" runat="server" />
        <a href="#" onclick="loadMe()">Click Me, please.</a>
        </div>
    </form>
</body>

ブラウザのコンソールにエラーが表示されます"PageMethods is not defined"

4

2 に答える 2

1

次のように使用できます。

$('a').click(function () {

        var data = { userName: $(this).attr("id") };
        var dataVal = JSON.stringify(data);

        $.ajax({
            type: "POST",
            url: "Default.aspx/loadNewPage",
            contentType: "application/json; charset=utf-8",
            data: dataVal,
            dataType: "json",
            success: function (id) {
                alert("loaded");
            }
        });
    });

[WebMethod]
public static void loadNewPage(int id)
{
  // do the loading of the new HTML, etc.  
}

ここでJquery AJAX 呼び出しについて詳しく知ることができます。

于 2013-09-04T08:41:16.317 に答える