Persisting hidden fields.

V

Velislav

Hi,

I have the following scenario:

A data-bound repeater, in which every item registers a hidden field
with the item's associated ID (a field pulled out of the database)
using the RegisterHiddenField method in the repeater's OnItemDataBound
event.

I do not re-bind the repeater on post-backs so as not to overwrite the
user input for dynamically created controls in the repeater. Which
means that the hidden fields will not be registered after the first
post back.

Ideally I wouldn't even need more than one postback - client-side
scripts verify input and the user is redirected to a new page when
clicking a button. However, should an exception be thrown at the server
side (eg database unique key violations etc), the user should be able
to correct the issue without having to capture everything again.

I wonder if there is a way to make the hidden fields persist across
post-backs (or alternatively another way to keep track of the repeater
items' associated IDs without displaying them).

I have tried the following, to no avail:
- add a HtmlInputHidden control to the page instead of using
RegisterHiddenField.
- bind the repeater in OnInit instead of Page_Load

Where does the OnItemDataBound even fit in the page's lifecycle?
 
V

Velislav

Ok I found the solution:

the hidden field must be defined in the repeater's item template,
something like this:
<input type="hidden" id="itemID" value="" runat="server/>

then in the ItemDataBound event handler, merely assign the control its
value:
DataRow row = ((DataRowView)e.Item.DataItem).Row;
HtmlInputHidden itemID = (HtmlInputHidden)e.Item.FindControl("itemID");
resolutionOptionID.Value = row["ID"].ToString();
 
V

Velislav

oops, that last line should've been:

itemID .Value = row["ID"].ToString();

:)

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top