在 ASP.NET 中将 DataTable 绑定到 Repeater 控件
Binding a DataTable to a Repeater control in ASP.NET
BusinessLogic.EventType :
namespace BusinessLogic
{
public class EventType : IModel
{
public int EventTypeID;
public string Name;
public string Description;
public string Photo;
}
}
我的 Aspx:
<asp:Repeater ID="rptr" runat="server">
<ItemTemplate>
<div class="col-lg-4 col-md-4 col-sm-4 mb">
<a href="#">
<div style="background-image: url('<%# Eval("Photo") %>'); height: 300px; display: block; background-size: cover; background-repeat: no-repeat; z-index: 1; left: 0; right: 0">
</div>
<div style="text-align: center; font-family: 'Comic Sans MS'; font-size: 20px; color: #db2828">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</div>
</div>
</a>
</ItemTemplate>
</asp:Repeater>
隐藏代码:
public partial class PastOrders : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["vendor"] != null)
{
if (!IsPostBack)
{
List<EventType> lstEventType = new List<EventType>();
EventTypeLogic eventTypeLogic = new EventTypeLogic();
DataTable dt = eventTypeLogic.SelectAll();
lstEventType = (from rw in dt.AsEnumerable()
select new EventType()
{
Name= Convert.ToString(rw["Name"]),
Photo = Convert.ToString(rw["Photo"])
}).ToList();
rptr.DataSource = lstEventType;
rptr.DataBind();
}
}
else
{
Response.Redirect("VendorLogin.aspx");
}
}
}
现在我已经使用了其中一个答案中提供的代码。编译好。但是会产生一个运行时错误:System.Web.HttpException: DataBinding: 'BusinessLogic.EventType' does not contain a property with the name 'Photo'.
我的 tblEventType
有以下列:EventTypeID
、Name
、Description
和 Photo
。
不知道怎么回事!
像这样使用 Named Arguments
for (int i = 0; i < dt.Rows.Count; i++)
{
lstEventType.Add(new EventType(){
EventType=dt.Rows[i]["Name"].ToString()
});
}
for (int i = 0; i < DataTable dt.Rows.Count; i++)
{
lstEventType.Add(dt.Rows[i]["Yourcolumn"].ToString());
}
我假设您有如下 EventType
class,
public class EventType
{
public string Name{get;set;}
public string Value {get;set;}
... more properties
}
并且您想将数据库值添加到事件类型中,希望您正在为您的 EventType
class
创建对象列表
你可以这样做
lstEventType = (from rw in dt.AsEnumerable()
select new EventType()
{
Name= Convert.ToString(rw["Name"]),
Value= Convert.ToString(rw["Value"]),
... more properties
}).ToList();
BusinessLogic.EventType :
namespace BusinessLogic
{
public class EventType : IModel
{
public int EventTypeID;
public string Name;
public string Description;
public string Photo;
}
}
我的 Aspx:
<asp:Repeater ID="rptr" runat="server">
<ItemTemplate>
<div class="col-lg-4 col-md-4 col-sm-4 mb">
<a href="#">
<div style="background-image: url('<%# Eval("Photo") %>'); height: 300px; display: block; background-size: cover; background-repeat: no-repeat; z-index: 1; left: 0; right: 0">
</div>
<div style="text-align: center; font-family: 'Comic Sans MS'; font-size: 20px; color: #db2828">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</div>
</div>
</a>
</ItemTemplate>
</asp:Repeater>
隐藏代码:
public partial class PastOrders : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["vendor"] != null)
{
if (!IsPostBack)
{
List<EventType> lstEventType = new List<EventType>();
EventTypeLogic eventTypeLogic = new EventTypeLogic();
DataTable dt = eventTypeLogic.SelectAll();
lstEventType = (from rw in dt.AsEnumerable()
select new EventType()
{
Name= Convert.ToString(rw["Name"]),
Photo = Convert.ToString(rw["Photo"])
}).ToList();
rptr.DataSource = lstEventType;
rptr.DataBind();
}
}
else
{
Response.Redirect("VendorLogin.aspx");
}
}
}
现在我已经使用了其中一个答案中提供的代码。编译好。但是会产生一个运行时错误:System.Web.HttpException: DataBinding: 'BusinessLogic.EventType' does not contain a property with the name 'Photo'.
我的 tblEventType
有以下列:EventTypeID
、Name
、Description
和 Photo
。
不知道怎么回事!
像这样使用 Named Arguments
for (int i = 0; i < dt.Rows.Count; i++)
{
lstEventType.Add(new EventType(){
EventType=dt.Rows[i]["Name"].ToString()
});
}
for (int i = 0; i < DataTable dt.Rows.Count; i++)
{
lstEventType.Add(dt.Rows[i]["Yourcolumn"].ToString());
}
我假设您有如下 EventType
class,
public class EventType
{
public string Name{get;set;}
public string Value {get;set;}
... more properties
}
并且您想将数据库值添加到事件类型中,希望您正在为您的 EventType
class
你可以这样做
lstEventType = (from rw in dt.AsEnumerable()
select new EventType()
{
Name= Convert.ToString(rw["Name"]),
Value= Convert.ToString(rw["Value"]),
... more properties
}).ToList();