Christopher Garcia
Christopher Garcia

Reputation: 2544

Changing WebControl Property Inside of a Repeater

I have a repeater control that contains an ItemTemplate containing a databound label and a DropDownList control. With each iteration, I'd like to change the ID of the DropDownList with each iteration, in order to use the values as inputs upon submission. Can anyone tell me how this is usually done? Thanks.

Upvotes: 0

Views: 162

Answers (2)

Christopher Garcia
Christopher Garcia

Reputation: 2544

I found it easiest to just put a PlaceHolder in the Repeater, then create the DropDownList controls at design-time, rather than trying to create one at design-time and changing the ID as it iterates.

Upvotes: 0

Bob
Bob

Reputation: 99694

This is done for you automatically, see the Using the NamingContainer Property to Determine a Control's Naming Container for details on how it works.

I imagine that probably isn't exactly what you are looking for though. In order to get the values from the drop downs, you should loop through the items in the repeater and use the FindControl method to find the drop down based on the ID you specified.

foreach(RepeaterItem item in repeater1.Items) {
    DropDownList dropdown = (DropDownList)item.FindControl("DropDownList1")
    //dropdown.SelectedValue
}

Edit: Based on your comment, I would not renamed the drop down list to be the ID in your database. Instead you should put a hidden literal control in your repeater. Set the value of that to the ID of your database record. Then get the ID the same way you get the value of the drop down list.

(Literal)item.FindControl("ListeralWithDataBaseId")

Your aspx markup will look like this

<asp:Literal runat="server" id="ListeralWithDataBaseId" 
     Value='<%# Eval("Id")' %> Visible="false" />

Upvotes: 1

Related Questions