我已经尝试了所有可以在网上找到的文章的组合,并且目前正在拔头发。下面的代码似乎甚至没有尝试进行数据绑定..当未对“引发异常”行进行注释时,它不会引发异常。
我尝试仅通过IEnumerable而不是datatable / datarows传递DataTable,但是我得到“ DataBinding:'System.Data.DataTable'不包含名称为'dateCompleted'的属性”。
任何帮助将不胜感激...
C#
protected void runReportForUser(Guid UserID)
{
pnCard.Visible = true;
pnNoPoints.Visible = false;
using (var db = new db())
{
// scorecard logic
var u = userProfile.get(UserID);
var t = db.Database.SqlQuery<DataTable>("exec ReturnEntities '" + u.userID + "'").FirstOrDefault();
//rptEvents.DataSource = t.ToArray()[0];
rptEvents.DataSource = t.Rows;
//rptEvents.DataSource = t;
rptEvents.DataBind();
}
}
protected void rptEvents_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//throw new Exception("Test")
DataRow dr = (DataRow)e.Item.DataItem;
((Label)e.Item.FindControl("lblDate")).Text = dr[0].ToString();
//((Label)e.Item.FindControl("lblDate")).Text = DataBinder.Eval(e.Item.DataItem, "dateCompleted").ToString();
((Label)e.Item.FindControl("lblCategory")).Text = dr[1].ToString();
((Label)e.Item.FindControl("lblName")).Text = dr[2].ToString();
((Label)e.Item.FindControl("lblPointsValue")).Text = dr[3].ToString();
}
}
ASPX:
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<th>Date</th>
<th>Category</th>
<th>Points Earned</th>
</tr>
<asp:Repeater ID="rptEvents" runat="server" OnItemDataBound="rptEvents_ItemDataBound">
<ItemTemplate>
<tr id="trEvents" runat="server">
<td><asp:Label ID="lblDate" runat="server" /></td>
<td><asp:Label ID="lblCategory" runat="server" /></td>
<td><asp:Label ID="lblName" runat="server" /></td>
<td><asp:Label ID="lblPointsValue" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
答案 0 :(得分:0)
在尝试了很多不同的东西后,我得以弄清楚。我最终不得不使其成为与表结构匹配的用户定义类的列表。看起来下面两种绑定方式都有效。
这里是为了帮助别人:
protected void runReportForUser(Guid UserID)
{
pnCard.Visible = true;
pnNoPoints.Visible = false;
using (var db = new db())
{
// scorecard logic
var u = userProfile.get(UserID);
var t = db.Database.SqlQuery<EntityHistory>("exec dbo.ReturnEntities '" + u.userID.ToString() + "'");
List<EntityHistory> EntityList = t.ToList<EntityHistory>();
rptEvents.DataSource = EntityList;
rptEvents.DataBind();
}
}
protected void rptEvents_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
EntityHistory EH = (EntityHistory)e.Item.DataItem;
((Label)e.Item.FindControl("lblDate")).Text = EH.dateCompleted;
((Label)e.Item.FindControl("lblCategory")).Text = DataBinder.Eval(e.Item.DataItem, "Category").ToString();
((Label)e.Item.FindControl("lblName")).Text = DataBinder.Eval(e.Item.DataItem, "Name").ToString();
((Label)e.Item.FindControl("lblPointsValue")).Text = DataBinder.Eval(e.Item.DataItem, "MaxPointsEach").ToString();
}
}
public class EntityHistory
{
public Guid UserID { get; set; }
public String dateCompleted {get;set;}
public String Category { get; set; }
public String Name { get; set; }
public Int32 MaxPointsEach { get; set; }
}