Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Finding controls using FindControl

Reply
Thread Tools

Finding controls using FindControl

 
 
Mufasa
Guest
Posts: n/a
 
      12-06-2007
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.


 
Reply With Quote
 
 
 
 
mohaaron@gmail.com
Guest
Posts: n/a
 
      12-06-2007
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" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Controls- Controls Collection-FindControl method Sundararajan ASP .Net Web Controls 2 03-10-2005 06:13 AM
Re: Using FindControl() to access controls created by javascript Lau Lei Cheong ASP .Net 2 12-15-2004 02:55 AM
Making FindControl find dynamic controls Michael ASP .Net 2 07-28-2004 10:13 AM
FindControl and user controls Merk ASP .Net 1 07-07-2004 11:54 PM
FindControl not finding dynamic user control - Repeater, Panel, Usercontrol theComputer7 ASP .Net Web Controls 5 06-24-2004 04:20 PM



Advertisments