将数据表绑定到ASP.NET中的Repeater控件 [英] Binding a DataTable to a Repeater control in ASP.NET

查看:236
本文介绍了将数据表绑定到ASP.NET中的Repeater控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BusinessLogic.EventType:

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'不包含名称为'Photo'的属性。

Now i have used the code provided in one of the answers. Compiles well . But produces a runtime error that : System.Web.HttpException: DataBinding: 'BusinessLogic.EventType' does not contain a property with the name 'Photo'.

我的 tblEventType 有以下列: EventTypeID 名称说明照片

my tblEventType has following columns : EventTypeID, Name, Description and Photo.

我不知道出了什么问题!

I do not know what is going wrong!

推荐答案

I假设您具有如下的 EventType 类,

I am assuming that you have the EventType class as below,

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

,并且您希望将数据库值添加到事件类型中,希望您为<$创建对象列表。 c $ c> EventType 类

and you want to add the database values to the event type hopefully you are creating the list of the objects for your EventType class

您可以这样做

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

这篇关于将数据表绑定到ASP.NET中的Repeater控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆