我有一个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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句