详细信息:我将 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"
。