Reputation: 744
How to call non static method from another page
I cant use of static method
I have a method in master page
Now, I want calling method in another page from master page method
Master Page:
protected void Pagination_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(DRCount.Text);
LinkButton LinkButton = (LinkButton)sender;
int Select = Convert.ToInt32(LinkButton.Text);
int Num2 = Count * Select;
int Num1 = Num2 - Count;
**//Calling GetData method in 01.aspx**
}
01.aspx.cs page:
public void GetData(int Num1, int Num2)
{
int Count = Convert.ToInt32(this.Master.Count);
int PriceSort = Convert.ToInt32(this.Master.Price);
string NameSort = this.Master.Name.ToString();...
}
Upvotes: 1
Views: 395
Reputation: 62074
If it's a method that's shared between various pages, it shouldn't really be declared in a specific page. Good design principles would say to make it part of a separate class. Let's say you call it "MyDataClass". (Obviously choose something more appropriate).
protected void Pagination_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(DRCount.Text);
LinkButton LinkButton = (LinkButton)sender;
int Select = Convert.ToInt32(LinkButton.Text);
int Num2 = Count * Select;
int Num1 = Num2 - Count;
MyDataClass dataClass = new MyDataClass();
dataClass.GetData();
}
And in another file called MyDataClass.cs
public class MyDataClass
{
public void GetData(int Num1, int Num2)
{
//...
}
}
That way you can use it in as many pages as you need, or even easily re-use it in another project. This sort of abstraction of responsibilities is one of the fundamental things to learn about object-oriented programming in languages such as C#.
Upvotes: 1
Reputation: 151730
You don't want to new
another ASP.NET page from codebehind. Instead move the code to a class in which you can utilize the shared logic.
Upvotes: 4
Reputation: 69
The access modifier needs to be changed from private to public. Additionally, all variables you need seek to update in the other method, need to be accessible. (Read codecasters answer for the more profound answer specific to your problem)
Make these changes:
protected void Pagination_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(DRCount.Text);
LinkButton LinkButton = (LinkButton)sender;
int Select = Convert.ToInt32(LinkButton.Text);
int Num2 = Count * Select;
int Num1 = Num2 - Count;
var otherClass = new OtherClass();
otherClass.GetData(Num1,Num2);
}
01.aspx.cs page:
public void GetData(int Num1, int Num2)
{
int Count = Convert.ToInt32(this.Master.Count);
int PriceSort = Convert.ToInt32(this.Master.Price);
string NameSort = this.Master.Name.ToString();...
}
I'm not sure what your other class is called, but this should work.
Upvotes: 0
Reputation: 6876
You could simply do
YourClassName variableName = new YourClassName();
Then call the method as variableName.GetDate(1,2);
.
You'll have to change the method from private
to public
in order to call it from another class.
Further reading on Access Modifiers
Upvotes: 1