Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET中将DataTable绑定到Repeater控件_C#_Asp.net_Datatable - Fatal编程技术网

C# 在ASP.NET中将DataTable绑定到Repeater控件

C# 在ASP.NET中将DataTable绑定到Repeater控件,c#,asp.net,datatable,C#,Asp.net,Datatable,BusinessLogic.EventType: namespace BusinessLogic { public class EventType : IModel { public int EventTypeID; public string Name; public string Description; public string Photo; } } 我的Aspx: <asp:Repeater

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.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(会话[“供应商”]!=null)
{
如果(!IsPostBack)
{
List lstEventType=新列表();
EventTypeLogic EventTypeLogic=新的EventTypeLogic();
DataTable dt=eventTypeLogic.SelectAll();
lstEventType=(来自dt.AsEnumerable()中的rw)
选择新的EventType()
{
Name=Convert.ToString(rw[“Name”]),
Photo=Convert.ToString(rw[“Photo”])
}).ToList();
rptr.DataSource=lstEventType;
rptr.DataBind();
}
}
其他的
{
重定向(“VendorLogin.aspx”);
}
}
}
现在我使用了其中一个答案中提供的代码。汇编得很好。但产生运行时错误:
System.Web.HttpException:DataBinding:“BusinessLogic.EventType”不包含名为“Photo”的属性。

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


我不知道出了什么问题

使用
命名参数执行类似操作

 for (int i = 0; i < dt.Rows.Count; i++)
{
lstEventType.Add(new EventType(){
EventType=dt.Rows[i]["Name"].ToString()

});
}
for(int i=0;i
for(int i=0;i
我假设您拥有如下
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();

那么您遇到了什么问题?
lstEventType.Add()
此部分。。我不知道应该如何遍历数据表并将该值分配给中继器。@CoderofCode:我希望我的问题对您来说是清楚的?您的
EventType
类的结构是什么?什么是
EventType
?您被声明为
string
属性,在上面的代码中,您将其用作
类型
?上面的代码编译了吗?我将添加表的两个属性:
Name
Image
。。仍然是相同的代码?我在这里使用的是三层架构。我的
BusinessLogic
中有一个C#file,其中声明了数据库中的所有变量。因此,现在我想将所有
事件类型添加到我的中继器控件中。请检查我的更新代码。请尝试此代码并让我知道。您必须更改
中的属性名称,根据
EventType
类中的实际属性名称选择
,我只想将数据库中的数据添加到repeater控件。特别是
名称
图像
值。我有一个实用方法
SelectAll
,它返回一个
datatable
。请检查我更新的代码。我已经提供了所有必要的代码。您提供的代码给了我一个运行时错误。请检查我的更新代码。
  lstEventType  = (from rw in dt.AsEnumerable()
               select new EventType()
               {
                   Name= Convert.ToString(rw["Name"]),
                   Value= Convert.ToString(rw["Value"]),
                   ... more properties 
               }).ToList();