Click here to Skip to main content
11,647,828 members (61,918 online)
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 20: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 XMLHTTPRequestto 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. inheritanceshould 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!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 : Smile | :) The Name of Web form in Method or Function Assume is Registration; Smile | :) Assume The Method or Function Name is EmpRegistration; Smile | :) Go to the Form and Calling Place you want to Access the Method or Function Smile | :) Then Type Registration.EmpRegistration (in Calling Place) Smile | :) Then Simple You can access Your Function or Method.

I Hope This will Help you. Thanks

  Permalink  
Comments
CHill60 at 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)

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 410
2 DamithSL 290
3 CPallini 290
4 OriginalGriff 277
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web01 | 2.8.150804.4 | Last Updated 16 May 2015
Copyright © CodeProject, 1999-2015
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