Skip to main content
 首页 » 编程设计

c#之从我的 ASPX 页面调用 C# 类

2024年02月05日8mengfanrong

我有一个 ASPX 页面,它是处理设计和代码背后的 Web 表单。我被告知我在这个页面中的所有类都需要分解成它们自己的文件。这些文件需要与类名同名。我的 ASPX 文件是 default.aspx,我的类是 GetMachineInfo.cs。我如何在我的 ASPX 文件中调用该 cs 文件,以便它就像我的类在 ASPX 页面中一样。如果您需要,我会插入一些代码。为了专注于实际问题,我删除了一些代码。如果缺少大括号或因此而导致的问题。只是想为这个特定问题提供必要的信息。干杯。

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(); 
        }

请您参考如下方法:

如果您的 GetMachineInfo() 方法定义在与您的表单相同的命名空间(即 PVDA.Web)中,您应该能够按预期调用它,但是方法需要定义在一个类,而不是直接在命名空间下。

因此,您可能需要考虑将它们标记为 static 以允许在不显式实例化您的类的实例的情况下调用它们:

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

这将允许您通过 Machine.GetMachineInfo(...) 在整个应用程序中调用此方法,您可以在应用程序的任何页面中执行此操作:

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