我正在寻找帮助在ASP.NET页面中使用对象作为GridView的数据源。场景是:我正在为我公司的员工创建一个度假计划员。我创建了一个类来保存与每一天相关的信息(当我将来引用时的Req1),以及一个用于保存List<>的类。请求类(Req2)。我的目标是允许用户将信息输入到数据输入表单中,当他们提交时,来自输入表单的数据存储在对象Req1中,然后将其存储在请求列表中,然后将其用作请求列表的数据源。一个GridView。一旦用户输入了他们想要起飞的所有日期,他们就会提交所有日期,系统将遍历每个请求并将记录插入数据库。 (我想这个提交将在课堂上处理。)
我的问题在于我对ASP.NET的理解。我认为我将数据存储在ViewState中,但当用户输入特定记录时,gridview不会在屏幕上显示信息。 ToDataTable()方法返回每个Req1对象中值的数据表。我将gridview的DataSource指向ToDataTable()方法,以表格形式返回请求。
我更喜欢使用有关使用对象数据源的教程,但我找不到适合我在线需求的东西。我希望有人能指出我正确的方向。
以下是Req1和Req2类的代码。某些值填充了虚拟数据。
[Serializable]
public class RTORequestOrder
{
private int _RTORequestId = 123;
private int _RTOOrderId = 123;
private DateTime _RTOOrderDate = DateTime.Now;
private string _RTOOrderRecipientEmpId = "123456";
private int _RTOOrderTimeOffCd = 123;
private string _RTOOrderStartTime = "4 O'Clock";
private int _RTOOrderDuration = 123;
private LunchBreak _RTOOrderLunchOffset = LunchBreak.Mins00;
private int _RTOOrderStatus = 1;
private int _RTOOrderState = 1;
private byte[] _RTOOrderVersion;
private string _RTOInsertUserId = "Donald";
private string _RTOInsertUserWorkstation = "MyComputer";
public int RTORequestId
{
// Set by calling object when this object is created.
get { return _RTORequestId; }
}
public int RTOOrderId
{
// Set by calling object when this object is created.
get { return _RTOOrderId; }
}
public DateTime RTOOrderDate
{
// Set by drop down.
get { return _RTOOrderDate; }
set { _RTOOrderDate = value; }
}
public string RTOOrderRecipientEmpId
{
get { return _RTOOrderRecipientEmpId; }
}
public int RTOOrderTimeOffCd
{
get { return _RTOOrderTimeOffCd; }
set { _RTOOrderTimeOffCd = value; }
}
public string RTOOrderStartTime
{
get { return _RTOOrderStartTime; }
set { _RTOOrderStartTime = value; }
}
public int RTOOrderDuration
{
get { return _RTOOrderDuration; }
set { _RTOOrderDuration = value; }
}
public LunchBreak RTOOrderLunchOffset
{
get { return _RTOOrderLunchOffset; }
set { _RTOOrderLunchOffset = value; }
}
public int RTOOrderStatus
{
get { return _RTOOrderStatus; }
}
public int RTOOrderState
{
get { return _RTOOrderState; }
}
public byte[] RTOOrderVersion
{
get { return _RTOOrderVersion; }
}
public string RTOInsertUserId
{
get { return _RTOInsertUserId; }
}
public string RTOInsertUserWorkstation
{
get { return _RTOInsertUserWorkstation; }
}
public RTORequestOrder(int RTORequestId, string RTOOrderRecipientEmpId, string RTOInsertUserId, string RTOInsertUserWorkstation)
{
// Set properties
_RTORequestId = RTORequestId;
_RTOOrderRecipientEmpId = RTOOrderRecipientEmpId;
_RTOInsertUserId = RTOInsertUserId;
_RTOInsertUserWorkstation = RTOInsertUserWorkstation;
}
public void InsertRTORequstOrder()
{
DALRTORequest.InsertRTORequestOrder(this);
}
}
[Serializable]
public class UserRequestOrders
{
private List<RTORequestOrder> m_thisUsersOrders = new List<RTORequestOrder>();
public List<RTORequestOrder> RequestOrders
{
get { return m_thisUsersOrders; }
}
public DataTable SelectRequestOrders()
{
DataTable dt = ToDataTable(m_thisUsersOrders);
dt.Columns.Remove("RTORequestId");
dt.Columns.Remove("RTOOrderLunchOffset");
dt.Columns.Remove("RTOOrderStatus");
dt.Columns.Remove("RTOOrderState");
dt.Columns.Remove("RTOOrderVersion");
dt.Columns.Remove("RTOInsertUserId");
dt.Columns.Remove("RTOInsertUserWorkstation");
dt.Columns["RTOOrderId"].ColumnName = "Order ID";
dt.Columns["RTOOrderDate"].ColumnName = "Date";
dt.Columns["RTOOrderRecipientEmpId"].ColumnName = "Name";
dt.Columns["RTOOrderTimeOffCd"].ColumnName = "Time Off Code";
dt.Columns["RTOOrderStartTime"].ColumnName = "Start Time";
dt.Columns["RTOOrderDuration"].ColumnName = "Duration";
return dt;
}
public void AddNew(RTORequestOrder newOrder)
{
m_thisUsersOrders.Add(newOrder);
}
private DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}
以下是我正在使用的页面背后的代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default3 : System.Web.UI.Page
{
RTODataEntryObjects.UserRequestOrders MyOrders = new RTODataEntryObjects.UserRequestOrders();
void Page_PreRender(object sender, EventArgs e)
{
ViewState.Add("vsMyOrders", MyOrders);
}
protected void Page_Load(object sender, EventArgs e)
{
NewDataGrid.EnableViewState = true;
if (ViewState["vsMyOrders"] != null)
{
MyOrders = (RTODataEntryObjects.UserRequestOrders)ViewState["vsMyOrders"];
}
//MyName.InnerText = User.Identity.Name;
//MyWorkstation.InnerText = System.Environment.MachineName;
}
public void btnSubmit(object sender, EventArgs e)
{
RTODataEntryObjects.RTORequestOrder thisOrder = new RTODataEntryObjects.RTORequestOrder(1, "Donald", User.Identity.Name, System.Environment.MachineName);
//Response.Write(thisOrder.RTOOrderId);
//MyOrders.AddNew(thisOrder);
ViewState["vsMyOrders"] = MyOrders;
}
}
最后,这是页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<label id="MyName" runat="server"></label>
<label id="MyWorkstation" runat="server"></label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSubmit" />
<fieldset id="RTORequestForm" runat="server">
<legend>Time Off Request</legend>
<asp:DataGrid ID="NewDataGrid" runat="server" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="ObjectDataSource1" GridLines="Horizontal">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right"
Mode="NumericPages" />
<AlternatingItemStyle BackColor="#F7F7F7" />
<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
</asp:DataGrid>
<%-- <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="RTODataEntryObjects.RTORequestOrder" InsertMethod="AddNew"
SelectMethod="SelectRequestOrders"
TypeName="RTODataEntryObjects.UserRequestOrders"></asp:ObjectDataSource>--%>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>
</fieldset>
</form>
</body>
</html>
答案 0 :(得分:1)
在添加自定义代码时,我找到了一个很好的搜索关键字“动态”。还可以尝试添加“Bind”和“List”。你看过http://msdn.microsoft.com/en-us/library/cc488549(VS.100).aspx页了吗?有一些代码会详细介绍您以及其他几个可能更接近您所需内容的链接。