473,406 Members | 2,217 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,406 software developers and data experts.

Finding controls using FindControl

I have a page that has a master page. I'm writing generic code to find
multiple controls on the page (I have a number of controls called tbName1,
tbName2, tbName3, ..., tbName20) and rather than doing all the same code for
all 20 fields, I thought I'd write something that loops through all of the
fields 'finding' the control as it goes. But if I do:
TextBox tbNew = (TextBox) this.FindControl("tbName1");

It comes back as null. If I look at the source generated within the browser,
the field is now called something like ctl00$MainPage$tbName2. Is there
anyway to tell the system to fine tbName2 without putting the prefix stuff
on it which I assume could change?

TIA - Jeff.
Dec 6 '07 #1
1 1316
Jeff,

I'm in the proecess of doing the exact same thing.

Try this.

ContentPlaceHolder masterPageContent =
(ContentPlaceHolder)Page.Master.FindControl("Conte ntPlaceHolderID");
Control control = (Control)masterPageContent.FindControl(controlId);
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
// Do something with your text box.
}

Aaron

On Dec 6, 11:58 am, "Mufasa" <j...@nowhere.comwrote:
I have a page that has a master page. I'm writing generic code to find
multiple controls on the page (I have a number of controls called tbName1,
tbName2, tbName3, ..., tbName20) and rather than doing all the same code for
all 20 fields, I thought I'd write something that loops through all of the
fields 'finding' the control as it goes. But if I do:

TextBox tbNew = (TextBox) this.FindControl("tbName1");

It comes back as null. If I look at the source generated within the browser,
the field is now called something like ctl00$MainPage$tbName2. Is there
anyway to tell the system to fine tbName2 without putting the prefix stuff
on it which I assume could change?

TIA - Jeff.
Dec 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob | last post by:
You can find datagrid in page by refering the form. Gatagrid is a child control of Form. Here is the code ----------------- Dim ctl As New Control For Each ctl In...
3
by: neverstill | last post by:
Hi- I have an asp:image control in the ItemTemplate of a DataList currently, in the ItemDataBound event, I am getting a reference to the image control by looping through all the controls in the...
3
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername";...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
2
by: hansiman | last post by:
When I want to add a js confirm box to a delete button that is positioned in the last column in a datagrid row I do the following in the ItemDataBound event: Dim delBtn As LinkButton =...
3
by: Andreas Wöckl | last post by:
Hi Group! I have a web form that is created dynamically - so I create Textboxes, RadioButtonLists, CheckBoxLists and so on - I have found some articles that there may be some problems with...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
5
by: Fred Chateau | last post by:
I am trying to reference a server control in a user control, from the containing page. Is there a way to do that? I have tried "UserControl1.FindControl("ControlName")" but I get a null reference....
1
by: David C | last post by:
I am trying to toggle a calendar control in a GridView and I am getting the error "Object reference not set to an instance of an object." The error is occurring in the 1st FindControl of the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.