我在ASPX页面中有一个HTML按钮单击事件的JavaScript函数.并在其代码页面后面的服务器方法.现在我想只在用户点击HTML按钮时用JavaScript函数调用服务器方法.
请不要更改此方案,也不要在回复时在aspx页面中使用任何asp.net contols.因为只允许HTML控件.谁可以帮我这个事 ?.提前致谢.急切地等待答案.
这是代码,
标记代码:
<script language="javascript" type="text/javascript">
function btnAccept_onclick() {
var name;
name = document.getElementById('txtName').value;
// Call Server side method SetName() by passing this parameter 'name'
</script>
<input type="button" id="btnAccept" value="Accept" onclick="return btnAccept_onclick()" />
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
public void SetName(string name)
{
// Code for some functionality
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有MasterPage的页面,在母版页中,我有这个:
<ajaxToolkit:ToolkitScriptManager ID="scriptManager"
runat="server"
AsyncPostBackTimeout="99999999"
EnablePageMethods="true" />
Run Code Online (Sandbox Code Playgroud)
我在后面的代码中有这个方法:
[WebMethod]
public void SavePreference(string graphVersion)
{
//some code here
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个javascript函数:
function lnkLearnHardWayclick(){
if( $("#chkDontShowAgain").attr("checked") == "checked")
{
PageMethods.SavePreference('new');
}
$("#info").hide();
$("#hardWay").show();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我点击链接以使一切正常时,我收到此错误:
未捕获的ReferenceError:未定义PageMethods