Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
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 4-Jul-12 21:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
  Permalink  
Comments
Mukunda Raj at 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 at 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 at 5-Jul-12 2:29am
   
can you can give he samle code
Mukunda Raj at 5-Jul-12 2:19am
   
Thank you!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Mukunda Raj at 5-Jul-12 2:16am
   
Thank you..
Could you please give an example how to add method to DLL and referencing them??
OriginalGriff at 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 at 5-Jul-12 2:30am
   
can you give a sample example code of building dll and use it.
OriginalGriff at 5-Jul-12 2:38am
   
See my reply above.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
public class Common
{
 public void MyFunction()
 {
 
  }
}
 
use this in you webform
 
web from 1 
public class webform1
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}
 
web from 2
public class webform2
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}
  Permalink  
Comments
Mukunda Raj at 5-Jul-12 2:35am
   
Thanks for sample code!!

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

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 5 Jul 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