Finding controls using FindControl

M

Mufasa

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.
 
M

mohaaron

Jeff,

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

Try this.

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

Aaron
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top