How to call method defined in Default.aspx.cs from another class
I am fairly new to asp.net and I am trying to create website. I have a class in AppCode/GSA in which I need to access event handler declared in default.aspx.cs. Can anyone help with it? I tried searching about it but all I got is standard method calls like classname.methodname() after creating an instance of that class. Please help me out here.
here is what I want to do and the reason why I need to call the method in default.aspx.cs
I am calling GSA functions to make html string which I am using to display search results in place holder. It works perfectly fine. Now I am adding button in front of every search displayed. When user clicks this button that button should be replaced by textfield and it will contain tiny url for that search result.
Now I am getting button with every result displayed, I have to assign event handler to that button. I tried putting that event handler in GSA.cs file itself but that function does not 开发者_如何学Pythonget called as it is not defined in default that why i decided to put that event handler in defalut.aspx.cs
Generally speaking, event handlers aren't the sort of thing that should be shared between classes. If you need to use the same functionality in two places, then pull that functionality out into its own method and invoke that method in your event handlers:
private void MyEventHandler(object sender, EventArgs e)
{
// some complex functionality
}
Should become:
public void DoComplexFunctionality()
{
// some complex functionality
}
private void MyEventHandler(object sender, EventArgs e)
{
DoComplexFunctionality();
}
The question of "where do I put DoComplexFunctionality()
?" is one that can only be meaningfully answered by someone who knows the specifics of what it does. You could make it a member of a static class, which would make it accessible to any code that can see that class. Or, if it's responsible for mutating a specific set of values, you could make it an instance member of a class that encapsulates those values, and pass an instance of that class to whoever needs to invoke the method.
In any case, trying to directly call an event handler is not the way to go.
You really should simply never be calling an event handler from other code.
I don't really have enough information to go on but I can envision two scenarios that might be giving you the idea that you should do this: a common page scenario and a shared business logic scenario.
If you are hoping to use some block of code in multiple pages, then I would strongly recommend that you create a parent class derived from the "Page" class and place all common code in there. This is NOT the same as a master-slave relationship; it is simply a way to implement code that all of your pages will use. I use my parent class to manage a strongly-typed session class, for example.
If you are looking to share or implement business logic and it just happens that you already put it in the Default.aspx.cs class, then you need to rethink your architecture. Even if you are not using an MVC approach, I would strongly recommend that you approach all but the most trivial of sites with a three-tier approach (once you've done it, additional work is trivial). In this case, you'll create classes that implement your business logic and only use your code-behind code for UI work. So your typical code-behind method will pull some stuff from the UI, create a business logic class and pass it in, and then process the results (or vice versa). In this case, you'll be able to call the business logic class that implements the code you want to share from anywhere - including your testing classes.
Drag your Default.aspx.cs to the AppCode folder with all your other classes and change the CodeFile
path in your Default.aspx to CodeFile="~/App_Code/Default.aspx.cs"
You should now be able to access it like any other class.
Is this what you want?
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string a = Class1.ctest();
Response.Write(a);
}
}
In App_Code/Class1.cs
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public Class1()
{
}
public static string ctest()
{
string test = "I need this";
return test;
}
}
Updated (code requested by the OP): Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string a = pick();
Response.Write(a);
}
protected string pick()
{
string test = "I need this";
return test;
}
}
精彩评论