How can i call [WebMethod] which is defined in user control?
Asked Answered
H

3

3

I am working in Asp.Net using C# and I have a [WebMethod] defined in a User Control. I don't want to use a web service.

How can I call a method with an [WebMethod] attribute? I want to call it from a jQuery (Ajax) request.

Hub answered 8/7, 2010 at 6:39 Comment(0)
B
4

You really should reconsider using a service if your goal is to centralize shared functionality.

ASMX services don't have any of the configuration burden that WCF services do (which is often what comes to mind when someone mentions "web service" now). If you use a simple ASMX "ScriptService", the code, development, configuration, etc will be nearly identical to what you're doing in ASPX files now, just centralized. ASMX ScriptServices and ASPX page methods are so similar that they literally share most of the same underlying backend code.

Basipetal answered 10/3, 2011 at 0:10 Comment(0)
N
4

You cannot call a page method declared within an ASCX user control. The page method would have to be declared in your aspx or expose it as a proper asmx web service.

Nonie answered 8/7, 2010 at 6:48 Comment(0)
B
4

You really should reconsider using a service if your goal is to centralize shared functionality.

ASMX services don't have any of the configuration burden that WCF services do (which is often what comes to mind when someone mentions "web service" now). If you use a simple ASMX "ScriptService", the code, development, configuration, etc will be nearly identical to what you're doing in ASPX files now, just centralized. ASMX ScriptServices and ASPX page methods are so similar that they literally share most of the same underlying backend code.

Basipetal answered 10/3, 2011 at 0:10 Comment(0)
S
1

You theoretically could, but it would get slightly messy and i wouldnt recommend it.
But what you could do is call the aspx page containing the control and pass a querystring parameter (like runfunc=1) which would be handled by the ascx control and run the function.
All other functionality would be run otherwise - and you could put all the html in a panel which is hidden if you called the "WebMethod" function.
This would also allow you to remove the WebMethod property on the function name.

Squilgee answered 8/7, 2010 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.