JQuery的,ASCX和webMethods不似乎是工作 [英] JQuery, ASCX and webmethods not seems to be working
问题描述
我有一个级联下拉(3人)类型,类别和子类别。类型的负载第一,在选择类型,类别负载和选择类别,子类别的负载。 另外我有2个按钮,添加目录和添加子类别当点击这些按钮,我叫一个jQuery模式窗体添加它们。我在code使用的WebMethod背后将它们添加到数据库
I have a cascading dropdown (3 of them) Type, Categories and Sub Categories. Type loads first and upon selection of Type, Category load and selection of Category, Sub Category loads. Also i have 2 buttons, "Add Category" and "Add Sub Category" Upon clicking on these buttons, i call a JQuery Modal Form to add them. I use Webmethod in code behind to add them to database
这完全在ASPX页面。
This works perfectly in ASPX page.
由于我需要使用3-4页,我认为进行上述的用户控件(ASCX)的。当我尝试使用此网页中,在ASCX的webMethods的不被调用。
Since I need use this in 3-4 pages, i thought of making the above as User control (ASCX). When i try to use this in a webpage, the webmethods in ASCX don't get called.
是我的做法是否正确?我应该为我的情况做
Is my approach correct? what should be done for my scenario
期待您的建议。
在此先感谢 KARTHIK
Thanks in advance Karthik
推荐答案
我不认为你可以有一个ascx控件内的WebMethod。 我解决它像这样我的问题:
i dont think you can have a WebMethod within a ASCX Control. I solved it for my Problem like this:
AJAXBridge:
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
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
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其作用基本上像所有的AJAX方法对所有Web控件界面。此外,还有一些优势就像概述和所有外露的WebMethods的控制权,这使得它更容易处理安全性问题(例如,与注释)。
So you have one ASPX which acts basically like a Interface for all AJAX Methods in all your Web Controls. There also some advantages like having overview and control of all exposed WebMethods, which makes it a lot easier to handle security matters (eg. with Annotations).
这篇关于JQuery的,ASCX和webMethods不似乎是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!