I am using WebForms .NET 4.5 and a asp:ListView and model binding. Within that I am using the:
<EditItemTemplate>
<InsertItemTemplate>
to define the appearance of the control. However 99% of the time these layouts are identical. Is there a way to use one for both INSERT and EDIT? Or is there another approach where I can define the HTML once and use it in either?
I am not using <asp:DynamicControl>
but normal <asp:texbox>
etc. so I don't believe .NET 4.5 WebForms: do I (still) really have to specify all 3 templates in a FormView? applies.
I have already tried a user control. And while the content gets included the model binding is broken in that no new values are applied to the object being inserted / edited.
SOLUTION UPDATE:
Declaring a form view without the INSERT TEMPLATE:
<asp:FormView ID="fvData" runat="server"
ItemType="DataLayer.Models.Country"
DataKeyNames="Id"
InsertMethod="InsertRecord"
SelectMethod="BindData"
UpdateMethod="UpdateRecord"
OnDataBound="fvData_DataBound">
<EditItemTemplate>
<b>EDIT</b>
<div class="row">
<div class="form-group">
<label class="col-md-4 control-label" for="txtCountryName">Name</label>
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtCountryName" name="txtCountryName" placeholder="My Country" CssClass="form-control" Text='<%#: BindItem.Name %>'></asp:TextBox>
<dav:DataAnnotationsValidator CssClass="label label-danger" ID="davSchoolName" runat="server" ValidationGroup="Default" MetadataSourceID="msCountryInformation" ControlToValidate="txtCountryName" ObjectProperty="Name" Display="Dynamic" />
</div>
</div>
</div>
</EditItemTemplate>
</asp:FormView>
and then:
protected void Page_Init()
{
if (!IsPostBack)
{
fvData.InsertItemTemplate = fvData.EditItemTemplate;
}
}
and there you are using the EDIT template for INSERT as well.