Saturday, March 24, 2012

Problem calling Web Service using the DynamicPopulate Extender - Web Service Call Fails: 5

Hi, I'm quite new to ASP.Net AJAX so please forgive my ignorance.

I have downloaded and installed the RC1 version and am following through some of the tutorial videos.

I am having a problem with the Dynamic Populate Extender example. I have created some asp controls with onclick events calling a javascript function. From there I am attempting to use a dynamic populate extender control to call a web service - it's a function declared on that ASPX page as the tutorial explains in necessary and decorated with the System.Web.Services.WebMEthod AND System.Web.Script.Services.ScriptMethod attributes.

Everything works as expected until I attempt to pass a parameter to the web services - in fact until I declare the web service function with a parameter. Once I do this, all that is returned to (or at least all that displays in) the control linked to the DynamicPopulateExtender is "Web Service Call Failed: 500".

Can anyone possibly put me on the right track here? (or would I need to post some code examples?)

Thanks in Advance.

Stu.

the tutorial is in beta version.

i will post my code for you

ASPX page

<%@. Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="DynamicPopulateDemo._Default" %>

<%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

</head>
<body>

<form id="form1" runat="server">
<script type="text/javascript">

function updateDateKey(value) {

var behavior = $find('dpe');

if (behavior) {
behavior.populate(value);
}

}

Sys.Application.add_load(function(){updateDateKey('G');});
</script>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:DynamicPopulateExtender ID="dpe" runat="server" ClearContentsDuringUpdate="true"
PopulateTriggerControlID="Label1" TargetControlID="pan" ServiceMethod="GetHtml">
</cc1:DynamicPopulateExtender>

<p>
<asp:Label ID="Label1" runat="server" Text="Label" CssClass="subheading">Time at the server:</asp:Label>
</p>
Choose a date/time format:<p>
<label for="r0"><input type="radio" name="rbFormat" id="Radio1" value='G' onclick="updateDateKey(this.value);" checked="checked" />Normal</label><br />
<label for="r1"><input type="radio" name="rbFormat" id="Radio2" value='d' onclick="updateDateKey(this.value);" />Short Date</label><br />
<label for="r2"><input type="radio" name="rbFormat" id="Radio3" value='D' onclick="updateDateKey(this.value);" />Long Date</label><br />
<label for="r3"><input type="radio" name="rbFormat" id="Radio4" value='U' onclick="updateDateKey(this.value);" />UTC Date/Time</label><br />
</p>
<br />
<p>

This time is dynamically formatted and<br /> returned as HTML from the server:

<asp:Panel ID="pan" runat="server" BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px" Height="15px" HorizontalAlign="Center" Width="380px">
</asp:Panel>
</p>

</form>


</body>

</html>

ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DynamicPopulateDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod()]
public static string GetHtml(string contextKey)
{
System.Threading.Thread.Sleep(250);
string value = string.Empty;

if (contextKey == "U")
{
value = DateTime.UtcNow.ToString();
}
else
{
value = String.Format("{0:" + contextKey + "}", DateTime.Now);
}
return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", value);


}
}
}


Thankyou very much for posting this code.

I have worked through the differences between your version of the tutorial and the code I was trying to get running. It all boiled down to the name of the parameter on the web service method. I don't understand this yet but if I call the parameter on my web service method, "contextKey" it works, if I call it anything else, it fails.

Is anyone able to shed some light on this for me?


In the documentation it states that the paramater must be astring namedcontextKey

[WebMethod]string DynamicPopulateMethod(string contextKey) { ... }

Note you can replace "DynamicPopulateMethod" with a naming of your choice, but thereturn type and parameter name and type must exactly match, including case.

No comments:

Post a Comment