我有一个级联下拉列表(其中3个)类型,类别和子类别。首先选择类型载荷,然后选择类型、类别载荷以及选择类别和子类别载荷。我还有两个按钮,“添加类别”和“添加子类别”单击这些按钮后,我调用一个JQuery模态表单来添加它们。我在后台代码中使用Webmethod将它们添加到数据库中
这在ASPX页面中工作得很好。
因为我需要在3-4个页面中使用它,所以我想把上面的作为用户控件(ASCX)。当我尝试在网页中使用它时,ASCX中的webmethods不会被调用。
我的方法正确吗?对于我的场景,应该做些什么
lOoking转发您的建议。
提前感谢Karthik
发布于 2010-06-01 18:39:58
我不认为你可以在一个ASCX控件中有一个WebMethod。我像这样解决了我的问题:
AJAXBridge:
namespace Demo{
public partial class AjaxBridge : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod(EnableSession = true)]
public static string Control_GetTest()
{
return Control.GetTest();
}
}}Control.ascx.cs
namespace Demo{
public partial class Control : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Session["test"] = DateTime.Now.ToString();
}
// ALMOST A WEB METHOD
public static string GetTest()
{
return " is " + HttpContext.Current.Session["test"];
}
}}Control.ascx
<script type="text/javascript">
var dataSend = {};
$.ajax({
type: "POST",
url: "AjaxBridge.aspx/Control_GetTest",
data: dataSend,
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function(data) {
alert("before");
},
success: function(data) {
alert("Page Load Time from Session " + data.d);
},
fail: function() {
alert("fail");
}
}); </script>因此,您有一个ASPX,它基本上就像是所有Web控件中所有AJAX方法的一个接口。也有一些优势,如拥有所有暴露的WebMethods的概述和控制,这使得处理安全问题变得容易得多(例如。带有注释)。
发布于 2010-04-25 13:38:10
您的ASP.NET方法在移动它之前,是不是像“在web网页中调用静态方法”小节here中描述的那样,在代码中标记了WebMethod属性的静态方法?如果是这样的话,这种类型的webmethod只适用于页面级别,不能在用户控件中使用。另一种选择是阅读this page的前两部分。
https://stackoverflow.com/questions/2704916
复制相似问题