Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

19 Experts available now in Live!

Get 1:1 Help Now