Previous Thread:   repeater control - change the background color depending on a value

3/20/2006 10:27:28 AM    FormView - FindControl only works for default view
Hi;  
  
I have the following two lines in my code-behind:  
  
object o = DataSourceItemView.FindControl("DataTitle"); // in <ItemTemplate>  
  
o = DataSourceItemView.FindControl("NewButton"); // in <InsertItemTemplate>  
  
When I run the first returns the control and the second returns null.  
  
If I then add DefaultMode="Insert" to my FormView, then the first returns a  
  
null and the second returns a control.  
  
It looks like FindControl only works for the default template - is this  
  
true? And if so, I assume this means FormView can only be used for one  
  
template?  
  
--  
  
thanks - dave  
  
david_at_windward_dot_net  
  
http://www.windwardreports.com



3/21/2006 7:59:15 AM    RE: FormView - FindControl only works for default view
Hi Dave,  
  
If you want to access the controls defined in a certain Template, the  
  
FormView¡¯s currentMode should be of that Template¡¯s mode. In other words,  
  
at a certain time, we can only access controls in a single template. Also,  
  
from the code you provided, I¡¯m still not quite sure about your detailed  
  
code logic, would you provide some further complete codebehind period?  
  
Regards,  
  
Steven Cheng  
  
Microsoft Online Support  
  
Get Secure! www.microsoft.com/security  
  
(This posting is provided "AS IS", with no warranties, and confers no  
  
rights.)

3/22/2006 2:57:25 AM    RE: FormView - FindControl only works for default view
Thanks for your response and the detailed code snippet and page template,  
  
From the complete code behind period, I think the problem is on the  
  
event(of the FormView) you're using, the event "ModeChanged" you're using  
  
is only a notification event on the changing of the detailsview's  
  
curerntMode, and at that time, the actual underlying template and control  
  
collection are still under construncting and the controls may not have been  
  
created yet.  To access the child controls in certain template, I suggest  
  
you use the "ItemCreated" event, at that time, the certain controls in the  
  
current active template are guaranteed to be created. e.g:  
  
======================  
  
protected void DetailsView1_ItemCreated(object sender, EventArgs e)  
  
{  
  
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)  
  
{  
  
Response.Write("<br/>" +  
  
DetailsView1.Rows[0].Cells[1].Controls[0]);  
  
}  
  
}  
  
==========================  
  
You can try using the FindControl to locate certain named control there.  
  
Hope this helps.  
  
regards,  
  
Steven Cheng  
  
Microsoft Online Support  
  
Get Secure! www.microsoft.com/security  
  
(This posting is provided "AS IS", with no warranties, and confers no  
  
rights.)