我遇到了一个问题,我似乎无法找到以下解决方案:
我有一个包含菜单的母版页,菜单中的每个链接都是一个 LinkButton。
我需要每当用户单击某个链接以显示登录弹出窗口时,我使用了 Ajax ModalPopupExtender 并成功显示了弹出窗口。
现在我想验证用户,这意味着我需要输入用户名和密码并按登录,但由于我在弹出窗口中,它会因为回发而关闭,所以我抑制了回发,现在我必须进行检查从客户端,所以我需要从 javascript 函数调用服务器方法,我尝试使用 PageMethods,但我一直没有定义 PageMethdos,然后我读到它在母版页或用户控件中不起作用。
我的问题有什么解决方案吗?
最佳答案
PageMethods 将在 aspx 页面中使用,而不是在 MasterPage 方法中使用。唯一的解决方法是创建一个单独的 .asmx WebService 并将您的逻辑添加到一个静态函数中。为此,请在 VS 上右键单击您的解决方案,然后单击“添加新项”,选择“WebService.asmx”。 在 WebService 的代码后面写一个静态的 webMethod
[WebMethod]// press alt+shift+f10 after selecting the WebMethod wording to include the library
public static bool CheckLogin (string username, string password){
//Type your method here
return result;//Result should be Boolean
}
现在在客户端脚本的 masterPage.master 上单击链接事件,向 Web 服务发布 Ajax 请求
$.ajax({
type: "POST",
url: "YourWebServiceName.asmx/CheckLogin",
data: '{"Username":"' + $('#username').val() + '","password":"' +
$('#password').val() + '"}',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(message) {
alert(message);//will alert 'true'
//DO what you want to do on client side
},
error: function() {
alert(message);//will alert 'false'
//DO what you want to do on client side
}
});
如果您需要进一步说明,请告诉我 祝你有美好的一天:)
关于c# - PageMethods 或母版页中的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19785219/