proseidon
proseidon

Reputation: 2305

Finding an item in a repeater control?

This one might be simple, but here it is anyway.

I have a List<Object> that I bind to a repeater. The repeater binds all of the fields to textboxes except Id. In my web page, whenever a user adds a new item, I create a new Object in my list with a unique Id and rebind my repeater.

At a certain point in my code, I am trying to read the textbox controls from the repeater and put them into my List<Object>. However, I need access to the Id field to know which List item to insert into. How can I get the specific Id while I'm going through the repeater?

I know I can just create a hidden field with the Id in the repeater control and get it that way, but is there a cleaner way to do this?

Example:

if (DependentRptr.Items.Count > 0)
{
    for (int count = 0; count < DependentRptr.Items.Count; count++)
    {
        int did = (form.UserId + (count + 1)); //I'm trying to get the id of this field here.
        ...get control info...

        var temp = AddedDependents.ToList().Find(p => p.Id == did); //here is where I search with the id
    }
}

Upvotes: 0

Views: 449

Answers (1)

Jupaol
Jupaol

Reputation: 21365

A Repeater is actually considered as a read-only control.

It is one of the simplest built-in data-bound controls.

It does not support actions like editing, inserting, deleting out of the box. If you want to use one of these actions in a repeater, you would have to write custom code to accomplish it. Even paging and sorting is not supported out of the box using a Repeater.

Therefore, there's no better way to accomplish your requirement while using a Repeater control, so a HiddenField would be a good way to fulfill your requirement.

However depending on your specific needs, you should consider using another data-bound control.

For example, the ListView control is also based on templates like the Repeater but it also supports common actions like editing, inserting and deleting

A ListView control contains the DataKeyNames property used to keep track of the ID's of each row

Upvotes: 1

Related Questions