Can"t access controls inside a formView
Can"t access controls inside a formView
am 16.01.2008 12:38:27 von Andy B
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.
Re: Can"t access controls inside a formView
am 16.01.2008 13:28:29 von Anthony Planz
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:eZzeIRDWIHA.1164@TK2MSFTNGP02.phx.gbl...
>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.
>
>
>
>
>
>
Re: Can"t access controls inside a formView
am 16.01.2008 14:08:51 von Eliyahu Goldin
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:eZzeIRDWIHA.1164@TK2MSFTNGP02.phx.gbl...
>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.
>
>
>
>
>
>
Re: Can"t access controls inside a formView
am 16.01.2008 14:13:53 von Andy B
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:EAEC06A4-EE5C-4563-9EB6-F7DF973EBBAC@microsoft.com...
> 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:eZzeIRDWIHA.1164@TK2MSFTNGP02.phx.gbl...
>>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.
>>
>>
>>
>>
>>
>>
>
Re: Can"t access controls inside a formView
am 16.01.2008 15:01:47 von Andy B
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:ueiVGEEWIHA.4548@TK2MSFTNGP03.phx.gbl...
> 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:eZzeIRDWIHA.1164@TK2MSFTNGP02.phx.gbl...
>>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.
>>
>>
>>
>>
>>
>>
>
>