I would like to change DynamicControl mode programatically in ASP.NET. I have already tried two methods but I failed both times. First i tried do perform it in code behind. Mode of DynamicControls is set to "Edit". On page load I iterated over controles and tried to change mode like this
((DynamicControl)c).Mode = DataBoundControlMode.ReadOnly;
This did not produce any results.
In second method I used inline expressions in aspx page.
Mode= "<%#getDynamicControlMode(MPFormView) %>"
and the function used in code behind is
public DataBoundControlMode getDynamicControlMode(FormView fv)
{
if (fv.CurrentMode == FormViewMode.ReadOnly)
return DataBoundControlMode.ReadOnly;
else if (fv.CurrentMode == FormViewMode.Edit)
return DataBoundControlMode.Edit;
else
return DataBoundControlMode.Insert;
}
This method failed also, controls stayed in ReadOnly Mode regardless of FormViewMode. I want to do this programaticall because I want to use only one template in FormView. Thanks
Finally i found out that i need to dig into DynamicControl to get underlaying controls. So i solved my problem using following code:
foreach (Control ct in cont.Controls)//cont is the DynamicControl
{
foreach (Control ci in ct.Controls)
{
if (ci is CheckBox)
((CheckBox)ci).Enabled = enable;
if (ci is TextBox)
((TextBox)ci).Enabled = enable;
}
}
This way i have disabled controls but it suits me as well.