Home > Mobile >  asp.NET Web App, how do I share classes with all code-behind files?
asp.NET Web App, how do I share classes with all code-behind files?

Time:07-30

I know that super-basic newbie questions aren't always welcomed on this site, and I'm afraid that the mods might swoop down on this post like a hawk on a hamster, but I'm really stuck.

I inherited an asp.NET application written in C#. It is not a project and not associated with a solution; it is simply a collection of aspx files and their associated aspx.cs code-behind files.

It's a programming mess, and I'm trying to rewrite it using class objects (I'm teaching myself this as I go). My class objects work fine, but I have to put the classes in each code-behind page; this is clearly not right.

What is the most straightforward way to place my classes in a single myClasses.cs file and let my other aspx.cs code-behind files use them? I've looked at a hundred different posts on this, but I can't seem to get at this one very simple question. Many of them involve projects within a solution, but as I said, this app is just a collection of files without a project. I tried to "build" in the terminal (I'm using VSCode), but it says it can't find a project file.

After looking at dozens of articles, this seems to be a generic answer. This is from here: enter image description here

Now, if app_code ALREADY exists, then you don't need to create that folder, and can simple add your class(s) to that folder - they will be global.

So, then this - right click on app_code, and add a class:

eg this:

enter image description here

At that point you can crete a class - either a static one for your hodge podge of genreal routines and code we have. And also classes that might have a mix of propertes and code - those you of course will have to create a instance of.

So, I have a system wide class - static - really amounts to a code module of general routines I need and use all the time.

So, for example, we OFTEN have to get a data table - no need to re-type that kind of code over and over.

So, in my code "general" class, I have this (and much more).

public static class General

{ 

    public  static DataTable MyRst(string strSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(strCon()))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                cmdSQL.Connection.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }

So, now on a web page, I can say drop in a grid view like this:

        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>

And now my page load code is this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        LoadGrid();
}

void LoadGrid()
{
    string strSQL =
        @"SELECT FirstName, LastName, HotelName, Description 
        FROM tblHotelsA
        ORDER BY HotelName";

    DataTable rstData = General.MyRst(strSQL);
    GridView1.DataSource = rstData;
    GridView1.DataBind();

And we get this:

enter image description here

So, since it is a static class - then I don't have to create a instance of that class before I use it - it just a bunch of handy code routines - and they are global to your code.

  • Related