Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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