簡體   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