Can't access controls inside a formView

Discussion in 'ASP .Net' started by Andy B, Jan 16, 2008.

  1. Andy B

    Andy B Guest

    I have 2 TextBoxes and a Label inside the EditItemTemplate of a formView
    (the EditItemTemplate is the only template it has). I need to access the
    properties of the Label and TextBox controls elsewhere on the page. How do
    you do something like that? I tried probably just about everything I can
    think of. I tried this line of code but I keep getting a null reference
    exception.

    Label DateConfirmLabel =
    (Label)EditNewsForm.Controls[0].FindControl("DateLabel");



    The really strange thing, this worked when I needed my Menu from the
    masterPage:



    Menu MainMenu = (Menu)this.Master.FindControl("MainMenu");



    Then I could access the MenuItems normally. Help!





    The FormView is inside a wizard.
     
    Andy B, Jan 16, 2008
    #1
    1. Advertising

  2. Hello Andi,
    if the controls resides in a content page (connected to a Masterpage) then
    the id's changing to something like
    ctl00$ContentPlaceHolder1$label.
    to see what's happens enable trace in the page directive.

    Kind Regards

    Anthony



    "Andy B" <> wrote in message
    news:...
    >I have 2 TextBoxes and a Label inside the EditItemTemplate of a formView
    >(the EditItemTemplate is the only template it has). I need to access the
    >properties of the Label and TextBox controls elsewhere on the page. How do
    >you do something like that? I tried probably just about everything I can
    >think of. I tried this line of code but I keep getting a null reference
    >exception.
    >
    > Label DateConfirmLabel =
    > (Label)EditNewsForm.Controls[0].FindControl("DateLabel");
    >
    >
    >
    > The really strange thing, this worked when I needed my Menu from the
    > masterPage:
    >
    >
    >
    > Menu MainMenu = (Menu)this.Master.FindControl("MainMenu");
    >
    >
    >
    > Then I could access the MenuItems normally. Help!
    >
    >
    >
    >
    >
    > The FormView is inside a wizard.
    >
    >
    >
    >
    >
    >
     
    Anthony Planz, Jan 16, 2008
    #2
    1. Advertising

  3. Depending what template is the default at the time of call, one of these
    should work:

    Label DateConfirmLabel = (Label)EditNewsForm.FindControl("DateLabel");

    or

    Label DateConfirmLabel =
    (Label)EditNewsForm.EditItemTemplate.FindControl("DateLabel");



    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Andy B" <> wrote in message
    news:...
    >I have 2 TextBoxes and a Label inside the EditItemTemplate of a formView
    >(the EditItemTemplate is the only template it has). I need to access the
    >properties of the Label and TextBox controls elsewhere on the page. How do
    >you do something like that? I tried probably just about everything I can
    >think of. I tried this line of code but I keep getting a null reference
    >exception.
    >
    > Label DateConfirmLabel =
    > (Label)EditNewsForm.Controls[0].FindControl("DateLabel");
    >
    >
    >
    > The really strange thing, this worked when I needed my Menu from the
    > masterPage:
    >
    >
    >
    > Menu MainMenu = (Menu)this.Master.FindControl("MainMenu");
    >
    >
    >
    > Then I could access the MenuItems normally. Help!
    >
    >
    >
    >
    >
    > The FormView is inside a wizard.
    >
    >
    >
    >
    >
    >
     
    Eliyahu Goldin, Jan 16, 2008
    #3
  4. Andy B

    Andy B Guest

    When I use EditNewsForm.Controls.Count I get 1 for the answer (probably
    because of EditItemTemplate). When I do EditNewsForm.Controls[0].Count I get
    3 for an answer (the Label and 2 TextBoxes). Since I figured this much out,
    I thought I could use EditNews.Controls[0].Controls[0] for the Label and
    EditNews.Controls[0].Controls[1]/[2] for the TextBoxes. When I actually Used
    the EditNewsForm.Controls[0].Controls[0] I ended up with index out of range
    error. How can that be when there are 3 controls inside of Controls[0]?

    Any detailed help on how to get to these controls...


    "Anthony Planz" <> wrote in message
    news:...
    > Hello Andi,
    > if the controls resides in a content page (connected to a Masterpage) then
    > the id's changing to something like
    > ctl00$ContentPlaceHolder1$label.
    > to see what's happens enable trace in the page directive.
    >
    > Kind Regards
    >
    > Anthony
    >
    >
    >
    > "Andy B" <> wrote in message
    > news:...
    >>I have 2 TextBoxes and a Label inside the EditItemTemplate of a formView
    >>(the EditItemTemplate is the only template it has). I need to access the
    >>properties of the Label and TextBox controls elsewhere on the page. How do
    >>you do something like that? I tried probably just about everything I can
    >>think of. I tried this line of code but I keep getting a null reference
    >>exception.
    >>
    >> Label DateConfirmLabel =
    >> (Label)EditNewsForm.Controls[0].FindControl("DateLabel");
    >>
    >>
    >>
    >> The really strange thing, this worked when I needed my Menu from the
    >> masterPage:
    >>
    >>
    >>
    >> Menu MainMenu = (Menu)this.Master.FindControl("MainMenu");
    >>
    >>
    >>
    >> Then I could access the MenuItems normally. Help!
    >>
    >>
    >>
    >>
    >>
    >> The FormView is inside a wizard.
    >>
    >>
    >>
    >>
    >>
    >>

    >
     
    Andy B, Jan 16, 2008
    #4
  5. Andy B

    Andy B Guest

    Both of those fail as well... keep coming up with a null object reference...
    could all this trouble be because the formview is inside a wizard?


    "Eliyahu Goldin" <> wrote in
    message news:...
    > Depending what template is the default at the time of call, one of these
    > should work:
    >
    > Label DateConfirmLabel = (Label)EditNewsForm.FindControl("DateLabel");
    >
    > or
    >
    > Label DateConfirmLabel =
    > (Label)EditNewsForm.EditItemTemplate.FindControl("DateLabel");
    >
    >
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Andy B" <> wrote in message
    > news:...
    >>I have 2 TextBoxes and a Label inside the EditItemTemplate of a formView
    >>(the EditItemTemplate is the only template it has). I need to access the
    >>properties of the Label and TextBox controls elsewhere on the page. How do
    >>you do something like that? I tried probably just about everything I can
    >>think of. I tried this line of code but I keep getting a null reference
    >>exception.
    >>
    >> Label DateConfirmLabel =
    >> (Label)EditNewsForm.Controls[0].FindControl("DateLabel");
    >>
    >>
    >>
    >> The really strange thing, this worked when I needed my Menu from the
    >> masterPage:
    >>
    >>
    >>
    >> Menu MainMenu = (Menu)this.Master.FindControl("MainMenu");
    >>
    >>
    >>
    >> Then I could access the MenuItems normally. Help!
    >>
    >>
    >>
    >>
    >>
    >> The FormView is inside a wizard.
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >
     
    Andy B, Jan 16, 2008
    #5
    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. John R. Lewis
    Replies:
    0
    Views:
    663
    John R. Lewis
    Jan 4, 2006
  2. Replies:
    2
    Views:
    7,387
    twostepted
    Apr 30, 2006
  3. wikkiwikkiwaa
    Replies:
    2
    Views:
    12,584
    wikkiwikkiwaa
    Aug 9, 2006
  4. Chris

    Formview within Formview

    Chris, May 19, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    533
    Chris
    May 19, 2007
  5. Ryan
    Replies:
    3
    Views:
    2,150
Loading...

Share This Page