开发者

Repeater Control with Objects?

开发者 https://www.devze.com 2023-01-26 02:40 出处:网络
I was told to use a Repeater control in what I am doingwhich is a \"Data Entry\" screen with ASP.NET controls -a standard \"address\" like form. In cases, the fields on the form will repeated twice, o

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; }

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?

 <开发者_JAVA百科;div class="row1">
    <div class="label">  
       ID: 
    </div>  
    <div class="value">
       <asp:Label ID="lblId" runat="server" />
    </div>


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

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

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

rptrMyRepeater.DataSource = MyEntityCollection;

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

rptrMyRepeater.DataBind();

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;
 }
}

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.


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" />

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


You can you should use the ItemTemplate to display the markup for each object, you can use the HeaderTemplate and FooterTemplate to display those also.

for example :

<asp:Repeater id="myRepeater" runat="server">
  <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "MyProperty1") %>,
    <%# DataBinder.Eval(Container.DataItem, "MyProperty2") %>,
    <br />
  </ItemTemplate>
</asp:Repeater>

after defining the templates, just set the datasource of the repeater to your collection and databind it

myRepeater.DataSource = myCollection;  
myRepeater.DataBind();


You can bind a repeater to any list. So you can add the original and changed objects you want to bind to a list and DataBind the objects to the repeater.

var hotelsList = new List<Ron>();
hotelsList.Add(originalRon);
hotelsList.Add(changedRon);
rptrHotels.DataSource = hotelsList;
rptrHotels.DataBind();

The properties in the Ron class will be bound like this:

<asp:Repeater ID="rptrHotels" runat="server">
    <ItemTemplate>
        <div>
            <div class="label">  
                RONId: 
            </div>  
            <div class="value">
                <%#Eval("RONId")%>
            </div>
        </div>
        ....
    </ItemTemplate>
</asp:Repeater>


Definitely doable, and no need to do it in compiled code -- you can just bind directly to stuff in your item template:

    <div class="label">  
        Hotel: 
    </div>  
    <div class="value">
        <%# DataBinder.Eval(Container.DataItem, "Hotel") %>
    </div>
    <asp:LinkButton ID="lnkMap" runat="server" Command='<%# ((MyClass)Container.DataItem).MyProperty %>'>Map</asp:LinkButton></div>
</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号