473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

FindControl() usage

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
3 1319
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
2
by: christof | last post by:
How to do it: My page: <asp:DataList ID="dataListRoleMembers" ...> .... <FooterTemplate> <asp:LinkButton ID="btnAddMember" runat="server"...
2
by: encoad | last post by:
Hi everyone, I'm slowly going mad with Masterpages and the FindControl. After a couple days of figuring out how to use the FindControl command from within a Masterpage, I still can't explain...
11
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to get the text of an item in a GridView, but am doing something wrong. Can someone help me with the correct C# statement I need? Below is my GridView and my attempt to get the control....
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
14
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of that control on a Page ("Defaul.aspx"). The control works fine. Now, I want to be able to use "FindControl()" from...
7
by: AAaron123 | last post by:
Me.FindControl("MissionScheduleID"), below returns null. Do you know what I'm doing wrong? Thanks ***In my .aspx file I have: asp:Content ID="Content3"...
4
by: Hillbilly | last post by:
Maybe this is or isn't some kind of bug but it sure is goofy and remains a mystery that really has me puzzled for two reasons... // goofy syntax functions as expected... Panel finalStepButton =...
9
by: AAaron123 | last post by:
I'm this far in determining the correct code to find a textbox I need to set. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.