Jordan Darland
Jordan Darland

Reputation: 23

Calling a C# class from my ASPX page

I have an ASPX page that is the web form that is handling the design and code behind. I was told that all the classes that I have in this page need to broken up into their own files. Those files need to have the same name as the class name. My ASPX file is default.aspx, and my class is GetMachineInfo.cs. How do I call that cs file in my ASPX file, so that it is like my class is inside of the ASPX page.I will insert some code if you need it. I cut some code out in order to focus on the actual issue. If there is a curly brace missing or something its because of that. Just wanted to give the necessary information for this specific problem. Cheers.

namespace PVDA.Web
{
    /*
   * LINQ Queries for binding
   * This is calling the wrong information
   * It's calling the n attribute of "srn", not the mach "n"
   * This is going a layer too deep. needs to be mach no srn
   */

    public class Machine
    {

        // Getting all the objects to the machine
        public int snsrN { get; set; }
        public string calctype { get; set; }
        public string sensName { get; set; }


        public static List<Machine> GetMachineInfo(XDocument xDoc, int machineNumber)
        {

            return xDoc.XPathSelectElements("./mmsdata/mill/mach")
                               .Where(x => x.Attribute("n").Value == machineNumber.ToString())
                               .Elements()
                               .Select(x => new Machine
                               {
                                   sensName = x.Value,
                                   snsrN = Convert.ToInt32(x.Attribute("n").Value),
                                   calctype = x.Attribute("calctype").Value
                               }).ToList();

        }
    }
}
namespace PVDA.Web
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Calling
                ddlBinding();
                NewQuery();
            }

           
           
        }

  public void ddlBinding()
        {
            // Getting the XML file imported in
            const string FILE_PATH = @"MillData.xml";
            // Set the file path
            XDocument xDoc = XDocument.Load(Server.MapPath(FILE_PATH));


            Machine machine = new Machine();


            // Setting a variable to bind the data into the dropdownlists
            var dropDownDataList1 = GetMillInfo(xDoc, "n");
            var dropDownDataList2 = machine.GetMachineInfo(xDoc, 2);

 // Bind Machines to DropDownList2
            DropDownList2.DataSource = machine.GetMachineInfo(xDoc, 2);
            // Set text / value properties
            DropDownList2.DataTextField = "snsrN";
            DropDownList2.DataValueField = "sensName";
            // Bind the contents to be reflected in the UI
            DropDownList2.DataBind();
        }

Upvotes: 2

Views: 3787

Answers (2)

Chow
Chow

Reputation: 116

Create a separate .cs file for class. It's best practice to have file name and class name are the same. It will be easy to track and if you want to modify any functionality just you can goto the file and modify.

  1. create a separate file for class
  2. Remove the class def inside the aspx page
  3. Add the class namespace in aspx page
  4. Instantiate the class Ex. class CLS = new class();

Upvotes: 0

Rion Williams
Rion Williams

Reputation: 76557

If your GetMachineInfo() method is defined in the same namespace as your form (i.e. PVDA.Web), you should be able to call it as expected, however methods are required to be defined within a class and not directly under a namespace.

As a result you may want to consider marking them as static to allow them to be called without explicitly instantiating an instance of your class :

public class Machine
{
    public static List<Machine> GetMachineInfo(XDocument xDoc, int machineNumber)
    {
        // Omitted for brevity
    }
}

This would allow you to call this method throughout your application through Machine.GetMachineInfo(...), which you could do within any of the pages in your application :

// Within your ddlBinding() method
DropDownList2.DataSource = Machine.GetMachineInfo(xDoc, 2);

Upvotes: 5

Related Questions