使用Web方法与母版页 [英] using web methods with master pages
问题描述
我已经在我的网站的所有网页,位于我的母版页上的功能,我想它从一些jQuery的Ajax方法运行。
I've got a function on all pages in my site which is located in my master page and I want it to run from some jQuery Ajax method.
我有一些code这样的时刻
I've got some code like this at the moment
jQuery的
$(document).ready(function() {
$("#test").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#test").text(msg.d);
}
});
});
});
HTML的母版页
HTML in Master page
<div id="test">Click here for the time.</div>
在后面Asp.Net code我的主人VB
Asp.Net code behind in my Master VB
<WebMethod> _
Public Shared Function GetDate() As String
Return DateTime.Now.ToString()
End Function
目前,除非我将Web方法的 Default.aspx的
VB页不起作用
Currently this doesn't work unless I move the web method to the Default.aspx
VB page
有没有办法我可以改变这部分
Is there a way I can change this part
url: "Default.aspx/GetDate",
要使用的母版页功能?
我试图将其更改为
url: "template.master/GetDate",
但是,这只是给了我一个404错误
But this just gives me a 404 error
任何想法?
在此先感谢
推荐答案
您的WebMethod code不能驻留在codebehind你的母版页。
Your webmethod code cannot reside in the codebehind for your master page.
我发现它更容易,包括在我的项目的事情,我需要从多个页面调用一个实际的Web服务或WCF服务。
I've found it easier to include an actual web service or WCF service in my project for things that I will need to call from multiple pages.
编辑:
要添加一个WCF服务到您的项目:
To add a WCF Service to your project:
- 右键单击该项目
- 选择[WCF服务],并给它一个名称(即Agent.svc)
- 设置了服务(见 HTTP://www.$c$ cproject.com/KB/aspnet/jQuery_To_WCF.aspx )
以上在这里的#1的几个例子...
More than a few examples here on the Stackoverflow...
希望有所帮助。
这篇关于使用Web方法与母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!