Solved

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

Posted on 2011-09-07
4
977 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:kmt333
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 36501084
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
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 500 total points
ID: 36501172
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
 
LVL 25

Expert Comment

by:Rouchie
ID: 36501228
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
 
LVL 4

Author Closing Comment

by:kmt333
ID: 36512129
thanks!  binding during prerender and then using request.params("item") to grab the data worked like a charm.

kmt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now