Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 5-Nov-12 20:24pm
Comments
manognya kota at 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?
aspnet_regiis -i at 6-Nov-12 1:36am
   
make a static method
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You declare the private method what you wants to call as static
[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
  Permalink  
v3
Comments
Tadit Dash at 6-Nov-12 1:50am
   
The second function "MyPrivateMethod" has to be static.
Milind Thakkar at 6-Nov-12 1:55am
   
Would you please explain why ?
Tadit Dash at 6-Nov-12 2:29am
   
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...
Milind Thakkar at 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
Tadit Dash at 6-Nov-12 2:53am
   
Most welcome, my pleasure.
Thanks Milind...
Tadit Dash at 6-Nov-12 2:55am
   
I think you also need to change the first line in your answer
"I fail to understand the purpose behind the question. You call any other method from WebMethod, just like you call any other function from any function.".
 
It would be better you change it a little bit...
 
Thanks Millind...
Tadit Dash at 6-Nov-12 3:06am
   
My vote of 5.

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100