Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert an ASP.NET page using Ajax webmethods to an Ajax-enabled servercontrol?
    primarykey
    data
    text
    <p>In <a href="http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/" rel="nofollow noreferrer">this tutorial I am reading</a>, Dave Ward creates a page that shows the server date in a label without using the update panel. </p> <p>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. </p> <p>I created the page, codebehind, and javascript exactly as the article indicated and got the sample to work.</p> <p>So, to start trying to convert this to a servercontrol, I moved Dave's Javascript for the page into a file, ~tests/JScript.js:</p> <pre><code> 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."; } </code></pre> <p>And put the following class in my App_Code:</p> <pre><code>namespace foo { /// &lt;summary&gt; /// Summary description for ServerControlTest /// &lt;/summary&gt; 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 = "&lt;span ID=\"Literal1\" runat=\"server\"&gt;test&lt;/span&gt;&lt;input id=\"Button1\" type=\"button\" value=\"button\" onclick=\"UpdateTime();\" /&gt;"; 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&lt;ScriptDescriptor&gt; IScriptControl.GetScriptDescriptors() { return null; } IEnumerable&lt;ScriptReference&gt; IScriptControl.GetScriptReferences() { ScriptReference reference = new ScriptReference(); reference.Path = ResolveClientUrl("~/tests/JScript.js"); return new ScriptReference[] { reference }; } #endregion } } </code></pre> <p>Now, in my sample page, when I click the button, I get this error: <strong>PageMethods is not defined [Break on this error] PageMethods.GetCurrentDate(OnSucceeded, OnFailed);</strong> </p> <p>How do I call GetCurrentDate from the clientscript that my control registers?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload