Don't use Page_Load
to bind or access your FormView
, instead use the FormView
's DataBound
event and the CurrentMode
property:
protected void FormView1_DataBound(object sender, System.EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.ReadOnly)
{
// here you can safely access the FormView's ItemTemplate and it's controls via FindControl
}
else if(FormView1.CurrentMode == FormViewMode.Edit)
{
// here you can safely access the FormView's EditItemTemplate and it's controls via FindControl
}
else if(FormView1.CurrentMode == FormViewMode.Insert)
{
// here you can safely access the FormView's InsertItemTemplate and it's controls via FindControl
}
}