Solved

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

Posted on 2011-09-07
4
986 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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