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

Is it possible to inherit the method? If yes,may I know how to call a function or method of one web form to other web form using asp.net to avoid code length.
Posted

I would recommend that you move the common code outside of these forms and use it from both the forms.

still if you need to call the method from page itself, you will have to use XMLHTTPRequest to call the page with some specific query string and get the result back as response. doing this would involve a lot of client side code.

The better option from design and usability standpoint would be to move the common code outside both the forms.

P.S. never use inheritance for code resue. inheritance should only be used when you have an is-a relationship between entities. (all other uses of inheritance is wrong by design ans sooner ot later surface a lot of problems)
 
Share this answer
 
Comments
Mukunda Raj 5-Jul-12 2:12am    
Thank you..
Could you please explain me how to use common code outside the page and how to call from common code with example??
Rahul Rajat Singh 5-Jul-12 2:16am    
have the method inside a separate class and put this in APP_Code folder. pass all this method needs as arguments. this method could be an instance method or static method based on what it does.
Mohd Imran Saifi 5-Jul-12 2:29am    
can you can give he samle code
Mukunda Raj 5-Jul-12 2:19am    
Thank you!!
The only time you would want to do that is if the method is static - if it wasn't then it would require an instance of the other form to work from as the this reference. Since you are unlikely to have that instance in a web situation, static is the only real way to go.

In that case, why not remove the method from both forms, and add it to a DLL which both reference? The advantage is that it separates common code from being tied to any particular form, so if you decide FormB is no longer needed, you can delete it without breaking FormA and FormE later on.
 
Share this answer
 
Comments
Mukunda Raj 5-Jul-12 2:16am    
Thank you..
Could you please give an example how to add method to DLL and referencing them??
OriginalGriff 5-Jul-12 2:35am    
Easy! It's just a class library project. There is a walkthough on MSDN: http://msdn.microsoft.com/en-us/library/cc668164.aspx
Mohd Imran Saifi 5-Jul-12 2:30am    
can you give a sample example code of building dll and use it.
OriginalGriff 5-Jul-12 2:38am    
See my reply above.
F#

Its better you create one class in App_code folder and than create instance of calss and than than call method of the class from both web from...that will do for you


Example

put this class in App_code folder

C#
public class Common
{
 public void MyFunction()
 {

  }
}


use this in you webform

C#
web from 1 
public class webform1
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}

web from 2
public class webform2
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}
 
Share this answer
 
Comments
Mukunda Raj 5-Jul-12 2:35am    
Thanks for sample code!!
1 : Function or Method(s) Should be have Public Static ;
2 : then Go the form you want to access this Method;
3 : then Access by FormName.MethodeName;

Example :
:) The Name of Web form in Method or Function Assume is Registration;
:) Assume The Method or Function Name is EmpRegistration;
:) Go to the Form and Calling Place you want to Access the Method or Function
:) Then Type Registration.EmpRegistration (in Calling Place)
:) Then Simple You can access Your Function or Method.

I Hope This will Help you.
Thanks
 
Share this answer
 
Comments
CHill60 18-May-15 3:38am    
Reason for my downvote - Static was mentioned in a Solution posted nearly 3 years ago and all 3 previously posted solutions (correctly) advise not doing it this way, but to separate the common code out.

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