use*_*114 3 .net javascript c# asp.net scriptmanager
我有一个 javascript 函数,用于 aspx 页面中的 HTML img 点击事件。其代码隐藏页面中还有一个服务器方法。现在我想仅当用户单击 HTML img 时才从 javascript 函数调用服务器方法而不带任何参数。
C# 代码隐藏方法:
[WebMethod]
public void PopUpClick(object sender, EventArgs e)
{
//Something;
}
Run Code Online (Sandbox Code Playgroud)
JavaScript方法:
$(document).ready(function () {
$('.clickme').click(function () {
PageMethods.PopUpClick();
});
});
Run Code Online (Sandbox Code Playgroud)
我还添加到母版页中:<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" EnablePageMethods="true" />
它不起作用。当我在 Chrome 上调试此 Javascript 函数时,我看到一个错误:未捕获的引用错误:PageMethods 未定义。
.aspx
<div>
<p>Say bye-bey to Postbacks.</p>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="HandleIT(); return false;" />
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript
<script type="text/javascript">
function HandleIT() {
var name = document.getElementById('<%=txtname.ClientID %>').value;
var address = document.getElementById('<%=txtaddress.ClientID %>').value;
PageMethods.ProcessIT(name, address, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Something wrong.');
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
C#
[WebMethod]
public static string ProcessIT(string name, string address)
{
string result = "Welcome Mr. " + name + ". Your address is '" + address + "'.";
return result;
}
Run Code Online (Sandbox Code Playgroud)