Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i need to create a webmethod and a normal method and need to call the normal method from the webmethod.

I just need a small Example for how to do this task.Can anyone help me in doing this..??
Posted
Comments
manognya kota 6-Nov-12 1:34am    
Just for me to get an idea, can i know why do you want to call a asp method from a web method?
bbirajdar 6-Nov-12 1:36am    
make a static method

I agree with @aspnet_regiis.

The same type of question is discussed here[^].

The WebMethods are always atatic.
For details check the link below.

So, why do page method calls have to be static?

If you’re implementing page methods, you’re probably well aware of their excellent performance. They are especially performant compared to the UpdatePanel’s partial postbacks.

They are efficient largely because they do not require the ViewState to be POSTed and they do not create an instance of the Page, while partial postbacks do both of these things. As we now know, a page method couldn’t create an instance of the page even if desired, since the ViewState isn’t provided in the request.

This is exactly why they must be marked as static. They cannot interact with the instance properties and methods of your Page class, because a page method call creates no instance of the Page or any of its controls.

Page methods are roughly equivalent to shorthand for standalone web services. In fact, the ScriptManager even calls them exactly as it would a regular web service.
Go through the link and learn the fundamentals.

As the WebMethod is static, it cannot call one normal method of the Page class.
If you want to call one method inside the WebMethod, you need to mark it as Static.

Thanks...
 
Share this answer
 
You declare the private method what you wants to call as static
C#
[WebMethod]
public static string MyWebMethod(string postCode)
{
// My webmethod code
string str = MyPrivateMethod(2,5);
}

private static string MyPrivateMethod(int a, int b)
{
// My private method code
}

Sorry, If I have missed some other obvious question.

Thanks to Tadit Dash for pointing out the issues.

Milind
 
Share this answer
 
v3
Comments
The second function "MyPrivateMethod" has to be static.
MT_ 6-Nov-12 1:55am    
Would you please explain why ?
Sorry. I have also missed one thing in my previous comment.
That is - the webmethod also need to be static, which you have done wrong here.
And
as webmethods are always static, so, it cannot call one normal function.
It can only call one static function.

If you still have doubts, please try with the codes you have written and let me know, it works or not.

Thanks...
MT_ 6-Nov-12 2:43am    
yes, You are right. At first, I overlooked as simple web service. After your comment realized its a webmethod in aspx page.
Updated solution to declare both method as static. Thanks Tadit Dash
Most welcome, my pleasure.
Thanks Milind...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900