By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

FindControl() usage

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/ASPNETCont...aceholder.aspx

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"MattB" <so********@yahoo.com> wrote in message
news:35*************@individual.net...
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

Nov 19 '05 #2

P: n/a
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

Karl Seguin wrote:
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/ASPNETCont...aceholder.aspx

Karl

Nov 19 '05 #3

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"MattB" <so********@yahoo.com> wrote in message
news:35*************@individual.net...
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

Karl Seguin wrote:
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/ASPNETCont...aceholder.aspx

Karl

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.