I've always regretted the decision to use the .Net time-savers like FormView, DetailsView, and DataSource controls. Seems like there's an initial big win when I try and use them, in short-order I see nicely formatted, data populated controls on my web page, but when it comes down to the dirty work of implementing utilizing the complex application architecture I've built to support the app, I hit some detail that keeps me up all night cursing. But I never learn my lesson (or take the time to fully learn these convenience controls - best advice, just hand code the thing). But you and I will ignore even the best advice.
While working on an ASP.Net 2.0 Project Management System, I needed to set the SelectedIndex property of some DropDownList controls. The controls were contained in the EditItemTemplate of a FormView control. The FormView was bound to an ObjectDataSource populated by a List (Issue is a custom type).
THIS IS THE CODE THAT FAILED TO FIND THE CONTROL
protected void FormView1_DataBound(object sender, EventArgs e)
{
Issue drv = (Issue)FormView1.DataItem;
((DropDownList)FormView1.FindControl("ddlPriority")).SelectedValue = drv.PriorityId.ToString();
((DropDownList)FormView1.FindControl("ddlStatus")).SelectedValue = drv.StatusId.ToString(); ;
((DropDownList)FormView1.FindControl("ddlMilestone")).SelectedValue = drv.MilestoneId.ToString();
((DropDownList)FormView1.FindControl("ddlCategory")).SelectedValue = drv.CategoryId.ToString();
((DropDownList)FormView1.FindControl("ddlAssignedTo")).SelectedValue = drv.AssignedUserName.ToString();
}
Errors galore. All DropDownLists turned up null. I read blog after blog where you can't find the controls in a formview edit template. But you can. The issueis that the FormView DataBound event represents the CURRENT MODE of the form view. So if the default view is ReadOnly, the EditTemplate controls do not exist at that state.
SOLUTION - TEST FORMVIEW MODE BEFORE FINDING CONTROLS:
if (FormView1.CurrentMode == FormViewMode.Edit)
{
Issue drv = (Issue)FormView1.DataItem;
((DropDownList)FormView1.FindControl("ddlPriority")).SelectedValue = drv.PriorityId.ToString();
((DropDownList)FormView1.FindControl("ddlStatus")).SelectedValue = drv.StatusId.ToString(); ;
((DropDownList)FormView1.FindControl("ddlMilestone")).SelectedValue = drv.MilestoneId.ToString();
((DropDownList)FormView1.FindControl("ddlCategory")).SelectedValue = drv.CategoryId.ToString();
((DropDownList)FormView1.FindControl("ddlAssignedTo")).SelectedValue = drv.AssignedUserName.ToString();
}
Great, problem solved. Well, not exactly. The FormView select parameter was controlled by a drop down list outside the FormView. When the FormView was in Edit mode, and the selected index of the controlling drop down was changed, more errors.
That was solved by placing the following code in the controlling DropDownList SelectedIndexChanged event:
protected void ddlProjectList_SelectedIndexChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
FormView1.ChangeMode(FormViewMode.ReadOnly);
}
}
Now, hours later, I'm batteling with 'No parameterless constructor defined for this object' when attempting to update the same damned FormView. nothing beats elegant hand coding. If ever I choose the time-saver controls again, remind me to jump out a (basement) window.