asp.net cannot retrieve value of textbox inside placeholder inside repeater

Please help.  

I am using a repeater to generate a dynamic quiz.  Each line in the repeater is represented by a custom user control named question.  Each question has a placeholder (ID=placeHolderInput) for the input field which writes the input field based on question type (i.e., question type 5 = textbox this is the example below).

                Dim tb As New TextBox
                placeHolderInput.Controls.Add(tb)
                tb.ID = "txtBoxInput"

The quiz displays fine and I can grab the data easily from the controls outside the placeholder, but the newly entered user input data inside the placeholder is eluding me.  When the user clicks submit, I loop through the repeater to grab values,

Dim eR As Repeater = myRepeater

For Each r As RepeaterItem In eR.Items
                Dim eqitem As controls_question = r.FindControl("question")
                Dim ph As PlaceHolder = eqitem.FindControl("placeHolderInput")

                Dim tBO as Textbox = ph.FindControl("txtBoxInput") -- this returns no value
                Dim tBOx as TextBox = eqitem.FindControl("txtBoxInput") -- this also returns no value
Next

Since the textbox has no value, I cannot grab the text within it.  I loop through repeaters all the time to assess values, but I have never added in the placeholder and this seems to be the issue.

Thanks,
kmt
LVL 4
kmt333Asked:
Who is Participating?
 
ivan_vaguninConnect With a Mentor Commented:
Hi! You should call databind for repeater before OnLoad, if you do it after Load stage (e.g. OnPreRender of event handler) then client data will not be restored in controls inside repeater. This is according to asp.net control lifecycle.
There are some workarounds on this:
http://www.spsamples.com/2011/06/aspnet-dynamically-populate-controls-on.html
http://www.spsamples.com/2011/09/aspnet-get-client-data-at-onload-event.html

But the common solution is to change page execution to populate controls according to page lifecycle

0
 
RouchieCommented:
This is one of the most popular-asked questions on ASP.NET.  The rule is that any control added dynamically will not persist on postback, and therefore using the normal methods its value can also not be retrieved.

Here is a post that details what you can do about it using the current method:  
http://forums.asp.net/t/1186195.aspx

The logic is complex to be fair.  I personally have never bothered to learn it in any great detail.  In our app (also for quizzes) we opted to just put all the fields in, then just show/hide the respective ones for each question type.  Not ideal I know, but it works.
0
 
RouchieCommented:
There's another approach whereby when the textbox is created, you make a note (in a page variable) of the repeater row numbers and question type ID's.  Then on postback, in the Init phase, you can iterate back and repopulate that way, rather than having to re-bind anything.
0
 
kmt333Author Commented:
thanks!  binding during prerender and then using request.params("item") to grab the data worked like a charm.

kmt
0
All Courses

From novice to tech pro — start learning today.