具有对象的中继器控制? [英] Repeater Control with Objects?

查看:55
本文介绍了具有对象的中继器控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我在做的事情中使用Repeater控件,它是带有ASP.NET控件的数据输入"屏幕-像表单这样的标准地址".在某些情况下,表单上的字段将重复两次,一次是原始值,一次是更改后的值.我以前没有使用过此控件,但似乎必须绑定到数据库.相反,我有一个通过存储库获取的Entity对象.我可以绑定到这样的对象吗?

I was told to use a Repeater control in what I am doing which is a "Data Entry" screen with ASP.NET controls -a standard "address" like form. In cases, the fields on the form will repeated twice, once for the original values, one for the changed values. I have not used this control before but it seems like I have to bind to a database. Instead, I have an Entity object that has been obtained via a Repository. Can I bind to an object like this?

[DataContract()]
    public class RON
    {
        [DataMember]
        public string Id { get; set; }
        [DataMember]
        public string Comments { get; set; }

然后,我有我的ASP.NET字段,该字段与CSS一起使用,以使这些字段对齐.我可以把它放在ItemTemplate中吗?

Then, I have my ASP.NET fields which I used the following with CSS to get the fields to line up. Can I put this in an ItemTemplate?

 <div class="row1">
    <div class="label">  
       ID: 
    </div>  
    <div class="value">
       <asp:Label ID="lblId" runat="server" />
    </div>

推荐答案

您可能想在 PageLoad 中做一些事情,例如:

You will likely want to do something in your PageLoad such as:

List<SomeBusinessObj> MyEntityCollection = Repository.Load();

然后,您可以使用转发器控件的 DataSource 属性将您的实体对象集合绑定到转发器控件.

You then can bind your entity object collection to the repeater control using the DataSource property of the repeater control.

rptrMyRepeater.DataSource = MyEntityCollection;

然后通过在该控件上调用 DataBind ,您的集合将绑定到该控件.

Then by calling DataBind on that control, your collection will become bound to that control.

rptrMyRepeater.DataBind();

然后,您所要做的就是使用Repeater的数据绑定事件将自定义属性应用于转发器的控件.

Then all you need to do is use the Repeater's databound event to apply your custom properties to the repeater's controls.

protected void rptrExample_OnItemDataBound(object Sender, RepeaterItemEventArgs Args)
{
 if (Args.Item.ItemType == ListItemType.Item || Args.Item.ItemType == ListItemType.AlternatingItem)
 {
  SomeBusinessObj Obj = (SomeBusinessObj)Args.Item.DataItem;

  Label LabelControl = (Label)Args.Item.FindControl("lblSomeLabel");
  LabelControl.Text = Obj.CustomProperty;
 }
}

将为每个行项目调用 ItemDataBound 事件,因此,每次调用该事件时,您将使用该行项目的控件和数据绑定对象.

The ItemDataBound event will be called for each row item, so each time it is called you will be working on the that row item's controls and databound object.

另一种方法是直接在标记内进行数据绑定,而不使用 OnItemDataBound 事件.

An alternative approach would be to directly databind within markup, not using the OnItemDataBound event.

<asp:Label Text='<%# ((MyObjects.SomeBusinessObj)Container.DataItem).CustomProperty %>' runat="server" />

直接强制转换避免了调用 DataBinder.Eval 以及反射的开销.

The direct cast avoids the call into DataBinder.Eval, and the overhead of reflection.

这篇关于具有对象的中继器控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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