Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same template for edit and insert in a list view

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.

like image 564
TheEdge Avatar asked Nov 13 '13 22:11

TheEdge


1 Answers

Your best bet would be to develop a custom ListView for yourself.

Use one template and init the ListView to use that one for the other. You may also need to customize the save button to switch the CommandName when in any of the modes.

The code mentioned here would guide you in the said direction.

https://stackoverflow.com/a/2202850/1355315

I think your problem is the same as in this thread.

like image 73
Abhitalks Avatar answered Sep 29 '22 01:09

Abhitalks