FindControl() usage

M

MattB

I have a datagrid that has some controls (TextBoxes and DropDownLists)
that are created dynamically at runtime. I've got the generation working
ok, but now I need to see what was entered or selected in those controls
once the form is submitted.

I've tried using FindControl unsuccessfully like this:

cName = "first_name" 'this is the ID used when the control was created

Dim tb as TextBox
tb = FindControl(cName)
----------

So I thought maybe I needed to use the DataGrid's id (dgGuest) like this:

tb = dgGuest.FindControl(cName)
----------

But in both cases the value of tb = nothing, so the control was not found.

If I look at the source of the page when it's loaded in a browser, my
textbox's id looks like this:

id="dgGuest__ctl2_first_name"

Can someone tell me where I've gone wrong? Do I need to refer to the
cell it's in (like myCell.FindControl(cName))? That will be trickier but
for now I need to know what will work. Thanks!

Matt
 
K

Karl Seguin

Matt,
Dynamically created controls must be recreated on postback. Their value
will be maintained, but if you don't recreate the control the value will
simply be thrown away.

One posibility is to move your dynamically created code to OnItemCreated
instead of OnItemDataBound
(http://openmymind.net/databinding/index.html#4.3), but since I've seen your
code, this won't be very easy consider the row data isn't available in
OnItemCreated.

Denis Bauer's free Dynamic Control Placeholder might be able to help..never
used it myself:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Karl
 
M

MattB

Thanks again. Hmmm. Kind of discouraging.

Could I perhaps use session state to hold my controls once created, and
re-create them from the session state in the OnItemCreated event? Any
other ideas or tips relating to my Session idea? I'd like to try and not
depend on anyone else's code since I need to maintain this myself.

Thanks again for all the advise. This is incredibly valuable!

Matt
 
K

Karl Seguin

i think the viewstate is the best place to hold such information as it's
scope is perfect for what you need (postback in a page). The control I
pointed to pretty much does this all for you (as I understand
it)...alternatively you could always look through the Request.Form
collection, but extracting it will be something of a hack.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top