如何转换ASP。使用Ajax webmethods实现支持Ajax的服务器控件的NET页面?

时间:2022-10-30 14:31:00

In this tutorial I am reading, Dave Ward creates a page that shows the server date in a label without using the update panel.

在我正在阅读的本教程中,Dave Ward创建了一个页面,该页面在标签中显示服务器日期,而不使用更新面板。

I am trying to learn how to create servercontrols that use ajax for partial postbacks where methods within the control are called from clientscript generated by the same control, and I think that learning how to convert this page to a server control would be a help me understand what servercontrols use instead of webmethods to expose their methods to clientscript.

我想学习如何使用ajax创建servercontrols部分回发,在控制方法从clientscript生成同样的控制,我认为学习如何将这个页面转换成服务器控制将是一个帮助我明白servercontrols使用clientscript而不是webmethods暴露他们的方法。

I created the page, codebehind, and javascript exactly as the article indicated and got the sample to work.

我按照文章的指示创建了这个页面、codebehind和javascript,并让这个示例正常工作。

So, to start trying to convert this to a servercontrol, I moved Dave's Javascript for the page into a file, ~tests/JScript.js:

因此,为了开始将其转换为servercontrol,我将Dave的页面Javascript转换为一个文件,~tests/JScript.js:

 function UpdateTime() {
   PageMethods.GetCurrentDate(OnSucceeded, OnFailed); 
 }

 function OnSucceeded(result, userContext, methodName) {
   $get('Literal1').innerHTML = result; 
 }

 function OnFailed(error, userContext, methodName) {
   $get('Literal1').innerHTML = "An error occured.";
 }

And put the following class in my App_Code:

并在App_Code中添加以下类:

namespace foo
{
    /// <summary>
    /// Summary description for ServerControlTest
    /// </summary>
    public class ServerControlTest : CompositeControl, IScriptControl
    {
        ScriptManager sm;
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                // Test for ScriptManager and register if it exists
                sm = ScriptManager.GetCurrent(Page);

                if (sm == null)
                    throw new HttpException("A ScriptManager control must exist on the current page.");

                sm.RegisterScriptControl(this);
                sm.EnablePageMethods = true;
            }

            base.OnPreRender(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Literal lit = new Literal();
            lit.Text = "<span ID=\"Literal1\" runat=\"server\">test</span><input id=\"Button1\" type=\"button\" value=\"button\"  onclick=\"UpdateTime();\" />";

            this.Controls.Add(lit);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
                sm.RegisterScriptDescriptors(this);

            base.Render(writer);
        }

        [WebMethod]
        public static string GetCurrentDate()
        {
            return DateTime.Now.ToString();
        }

        #region IScriptControl Members

        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            return null;
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            ScriptReference reference = new ScriptReference();
            reference.Path = ResolveClientUrl("~/tests/JScript.js");

            return new ScriptReference[] { reference };
        }

        #endregion
    }
}

Now, in my sample page, when I click the button, I get this error: PageMethods is not defined [Break on this error] PageMethods.GetCurrentDate(OnSucceeded, OnFailed);

现在,在我的示例页面中,当我单击按钮时,会得到这个错误:PageMethods没有定义[在这个错误上中断]PageMethods。GetCurrentDate(OnSucceeded OnFailed);

How do I call GetCurrentDate from the clientscript that my control registers?

如何从我的控件注册的clientscript调用GetCurrentDate ?

1 个解决方案

#1


2  

There is actually no fully encapsulated method for implementing AJAX callbacks against methods of a server control yet, as of v3.5. It's a very frustrating limitation.

实际上,还没有完全封装的方法来实现AJAX回调,以对抗服务器控件的方法,如v3.5。这是一个非常令人沮丧的限制。

The most common solution is to create an HttpHandler in your server control's assembly, then require that the handler be registered in the web.config. Look at how ASP.NET AJAX's ScriptResource.axd is wired up in the web.config in ASP.NET AJAX 1.0, for example.

最常见的解决方案是在服务器控件的程序集中创建一个HttpHandler,然后要求在web.config中注册该处理程序。看看ASP。AJAX的ScriptResource净。axd是连接在网络上的。配置在ASP。例如,NET AJAX 1.0。

#1


2  

There is actually no fully encapsulated method for implementing AJAX callbacks against methods of a server control yet, as of v3.5. It's a very frustrating limitation.

实际上,还没有完全封装的方法来实现AJAX回调,以对抗服务器控件的方法,如v3.5。这是一个非常令人沮丧的限制。

The most common solution is to create an HttpHandler in your server control's assembly, then require that the handler be registered in the web.config. Look at how ASP.NET AJAX's ScriptResource.axd is wired up in the web.config in ASP.NET AJAX 1.0, for example.

最常见的解决方案是在服务器控件的程序集中创建一个HttpHandler,然后要求在web.config中注册该处理程序。看看ASP。AJAX的ScriptResource净。axd是连接在网络上的。配置在ASP。例如,NET AJAX 1.0。