I am new to .NET and Web development too. I have website written in c#. I want to declare one class and in that class i want to declare one function and that function want to call on second page. So how to write code for that?
Say I have 2 pages. One is reference.aspx and second page is edituerprofile.aspx. In reference.aspx page i want to write class and in that class want to write one function. And that function want to call in edituserprofile.aspx.
How to write this?
pls help me.
Thanx in advance
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Please follow the following steps
1)add new class file to your app_code folder which is created by visual studio itself
2)write, what are all the methods/functions you need, write on that class file
3)create an object in your asp.net page for that class
4)call the methode/function of class by using object.method/function_name();
thats it
Simple code friend…
string user_name="Some_user",password="correct_password";
login(user_name,password)
{
class_name object=new class_name();
if(true==object.methode_name(user_name,password))
// do_something
else
// do_something
}
in your class file
class class_name
{
public bool methode_name(string user_name,string password)
{
//your code here
if(/*yout code here to validate user*/)
return true;
else
return false;
}
}
Method 2
Create a class as public in App_code.
Create object of that class, and then
get new instance of the class to that object.
Then you can access all public functions of that class by the object
Example
public class class1
{
public void function1()
{
// do your job
}
}
and in pages
class1 objclass1 = new class1(); objclass1.function1();
Method 3
There are multiple approaches, depending on what you need.
The quickest way is create an static class in App_Code, and add the method in there
public static class Utils
{
public static void MyMethod() { }
}
Then you can call this MyMethod everywhere.
Method 4
Try
public Page1: BaseClass
{
public void Method1()
{
base.BaseMethod();
}
}
public BaseClass: System.Web.UI.Page
{
//.. specify your method here
protected BaseMethod()
{
}
}
Method 5
Try this,
Define common class library. like below
public abstract class PageBase : System.Web.UI.Page
{
public static void AddDropDownDefaultValue()
{}
}
And you are to used in page class file like below.
protected void BindStatus()
{
AddDropDownDefaultValue();
}
Method 6
Don’t use the codebehind classes as repository for all methods. Use separate classes with meaningful names for that.
You cannot access instance methods from another page since you don’t have an instance of it (if you don’t have used Server.Transfer to get to the second page, then you could use cast the PreviousPage property to the actual type).
So you could access it if the method is static. However, i would move methods that you need at several places into their own classes anyway as already mentioned.
Method 7
A more concrete example of what you’re trying to do would be useful.
Otherwise you’ll get all sorts of answers, many of which will be off
the mark.You should put common code in the App_Code folder. You should also not
have any business logic inside a forms code-behind.The fact that you need one page to call a method in another page
indicates that you haven’t done this. Pages are for displaying and
interpreting actions, but they should not hold any of the business
logic.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0