简体   繁体   中英

ASP.NET form with repeaters that dynamically generate input controls

I have a repeater control that pulls back some data from a database and creates a radiobuttonlist for values related to each row. I don't know how many rows will be coming back, so I don't know how many radiobuttonlists there will be. This repeater is inside of a form, and once I click "Submit" on the form I need to get the values from the radiobuttons generated by the repeater.

Any thoughts on how to do this?

There are a few options.

One is to use a nested Repeater to display the RadioButtons.

The other is to create the controls during the original repeaters. Repeater.ItemDataBound event.

In either case, you need to be very aware of the Page Lifecycle .

You'll want to avoid databinding in the Page_Load event, unless the databinding is being done with an if(!Page.IsPostback) block. If you fail to check for postback, you'll run into issues where the Page_Load calls a DataBind() call that then wipes the values of the repeater before any event handlers can use it.

Alternatively, you can do your binding in Page_Init, which occurs BEFORE the viewstate can be applied, so you won't lose your values.

You can also use USer Controls for the RadioButtons. There's a previous post that covers how to use nexsted user controls within a Repeater here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM