views:

359

answers:

3

I've written a control in C# that overrides the built-in DropDownList control. For this I need a javascript resource included, which I'm including as an embedded resource then adding the WebResource attribute, which works fine.

However, I also need to reference a webservice, which I would normally include in the scriptmanager on the page like this

<asp:scriptmanager id="scriptmanager" runat="server">
<Services>
<asp:ServiceReference Path="~/Path/To/Service.asmx" />
</Services>
</asp:scriptmanager>

Is there any way to make the page include this reference in the code behind on the control I've created, similar to how it includes the embedded javascript file?

A: 

If you know the page the usercontrol is in you can do a ((PageName)this.Page).scriptmanager.Services.Add() from the user control

Dested
A: 

You can just add the javascript to call the webservice yourself:

Sys.Net.WebServiceProxy.invoke(url, methodName, useHttpGet, parameters, succeededCallback, failedCallback, userContext, timeOut);

http://www.asp.net/AJAX/Documentation/Live/ClientReference/Sys.Net/WebServiceProxyClass/WebServiceProxyInvokeMethod.aspx

The docs are for asp.net Ajax 1.0, but it's the same .net 3.5.

Robert C. Barth
+2  A: 

You can add a ScriptManagerProxy in the code or the markup of your control and add the service reference through it. The settings in the ScriptManagerProxy are merged with the "real" ScriptManager at compile time.

Rob Windsor