2016-06-08 109 views
1

我有一个ASPX页面,它是处理设计和代码背后的Web窗体。我被告知,我在这个页面中的所有类都需要分解成自己的文件。这些文件需要与类名称具有相同的名称。我的ASPX文件是default.aspx,我的类是GetMachineInfo.cs。如何在我的ASPX文件中调用该cs文件,以便它像我的类位于ASPX页面内。如果需要,我会插入一些代码。为了专注于实际问题,我删除了一些代码。如果有一个大括号丢失或因为这个原因。只是想给这个具体问题提供必要的信息。干杯。从我的ASPX页面调用C#类

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

回答

4

如果您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); 
+0

仍然获得的错误_defualt.Machine不包含GetMachineInfo的定义与“DropDownList2.DataSource = Machine.GetMachineInfo(XDOC,2);以及其在GetMachineInfo没有找到的XDocument,或任何变量...我认为它简单一些,我忘了添加诚实 –

+0

您需要确保您将绑定代码放在一个实际的方法中(例如'dllBinding()')。您可能还必须添加相应的using语句在你的外部定义中,确保它可以访问它需要的适当的类,试着右键点击XDocument类并在Visual Studio中检查一个“Resolve”选项,它应该自动添加适当的using语句。将你的'Machine'类包含在不同的文件中并参考也是如此。 –

+0

你可以[在这里看到一个Gist演示了这些分离的文件可能看起来像什么](https://gist.github.com/Rionmonster/708305a93bc6060b0c99efa7fb0494af)。 –

0

创建类单独的cs文件。文件名和类名相同是最好的做法。这将很容易跟踪,如果你想修改任何功能,只要你可以转到文件并修改。

  1. 创建
  2. 类一个单独的文件
  3. 删除aspx页面
  4. 内部类DEF添加类命名空间中的aspx页面
  5. 实例化类 防爆。 class CLS = new class();
+0

是的,进入了一个代码审查,他们告诉我做这个改变我的课程。这正是我期待做的。我应该在哪里放置安装。另外,当我尝试插入类名的代码段时,我收到错误。 –