繁体   English   中英

将DataTable绑定到ASP.NET中的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包含以下列: EventTypeIDNameDescriptionPhoto

我不知道出了什么问题!

我假设你有如下的EventType类,

 public class EventType
 {
    public string Name{get;set;}
    public string Value {get;set;}
    ... more properties 
 }

并且您希望将数据库值添加到事件类型中,希望您正在为EventType类创建对象列表

你可以这样做

  lstEventType  = (from rw in dt.AsEnumerable()
               select new EventType()
               {
                   Name= Convert.ToString(rw["Name"]),
                   Value= Convert.ToString(rw["Value"]),
                   ... more properties 
               }).ToList();

使用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());
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM