Can't access controls inside a formView

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.

Advertisements

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

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

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!