【发布时间】:2016-06-08 12:27:00
【问题描述】:
我有一个 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();
}
【问题讨论】: