我有一个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();
}
仍然获得的错误_defualt.Machine不包含GetMachineInfo的定义与“DropDownList2.DataSource = Machine.GetMachineInfo(XDOC,2);以及其在GetMachineInfo没有找到的XDocument,或任何变量...我认为它简单一些,我忘了添加诚实 –
您需要确保您将绑定代码放在一个实际的方法中(例如'dllBinding()')。您可能还必须添加相应的using语句在你的外部定义中,确保它可以访问它需要的适当的类,试着右键点击XDocument类并在Visual Studio中检查一个“Resolve”选项,它应该自动添加适当的using语句。将你的'Machine'类包含在不同的文件中并参考也是如此。 –
你可以[在这里看到一个Gist演示了这些分离的文件可能看起来像什么](https://gist.github.com/Rionmonster/708305a93bc6060b0c99efa7fb0494af)。 –