Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't access controls inside a formView

Reply
Thread Tools

Can't access controls inside a formView

 
 
Andy B
Guest
Posts: n/a
 
      01-16-2008
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.






 
Reply With Quote
 
 
 
 
Anthony Planz
Guest
Posts: n/a
 
      01-16-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>
>
>
>
>
>


 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      01-16-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>
>
>
>
>
>



 
Reply With Quote
 
Andy B
Guest
Posts: n/a
 
      01-16-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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.
>>
>>
>>
>>
>>
>>

>



 
Reply With Quote
 
Andy B
Guest
Posts: n/a
 
      01-16-2008
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" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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.
>>
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically Enable/Disable Controls Inside FormView Ryan ASP .Net 3 09-21-2009 02:02 PM
Formview within Formview Chris ASP .Net 0 05-19-2007 04:12 PM
access controls inside formview inside loginview wikkiwikkiwaa ASP .Net 2 08-09-2006 06:27 AM
access inner formview controls Howard ASP .Net 3 07-31-2006 12:38 PM
FormView/Repeater/ Eval(FormView data) David Thielen ASP .Net Web Controls 2 06-16-2006 07:56 PM



Advertisments