Can't access controls inside a formView

A

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

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
 
E

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
 
A

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 said:
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 said:
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.
 
A

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top