Finding controls using FindControl

Discussion in 'ASP .Net' started by Mufasa, Dec 6, 2007.

  1. Mufasa

    Mufasa Guest

    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.
    Mufasa, Dec 6, 2007
    #1
    1. Advertising

  2. Mufasa

    Guest

    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

    On Dec 6, 11:58 am, "Mufasa" <> 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.
    , Dec 6, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Merk

    FindControl and user controls

    Merk, Jul 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    14,217
    Scott Allen
    Jul 8, 2004
  2. Michael
    Replies:
    2
    Views:
    7,967
    =?Utf-8?B?YnJpc2Vycw==?=
    Jul 28, 2004
  3. Lau Lei Cheong
    Replies:
    2
    Views:
    5,071
    Lau Lei Cheong
    Dec 15, 2004
  4. theComputer7
    Replies:
    5
    Views:
    1,318
    theComputer7 - no spam
    Jun 24, 2004
  5. Sundararajan

    Custom Controls- Controls Collection-FindControl method

    Sundararajan, Mar 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    182
    Sundararajan
    Mar 10, 2005
Loading...

Share This Page