10

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.

Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • Would the user control work if you use `<%# Bind("Path") %>` instead of `<%# BindItem.Path %>`? – Richard Deeming Dec 02 '13 at 16:43
  • @RichardDeeming Wouldn't that mean that model binding would no longer work as using BindItem is required for Model Binding? – TheEdge Dec 02 '13 at 20:52
  • I don't think so. AFAIK, either syntax should work with model binding. – Richard Deeming Dec 03 '13 at 12:08
  • @RichardDeeming Tried Bind() syntax and exhibits the same behaviour as BindItem ie. It shows the data but does not update it back to the model and hence the database. – TheEdge Dec 06 '13 at 06:01

2 Answers2

3

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.

Community
  • 1
  • 1
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • Thanks to @abhitalks for making me look at that suggested solution again. I had previously looked at it, tried it and dismissed it as it did not work (that was with a list view). However trying it again with a FormView and it worked like a charm as per my updated original post. – TheEdge Dec 06 '13 at 06:38
0

Maybe you should extract this common layout as a separate control. I do this in ASP.Net MVC.

Ark-kun
  • 6,358
  • 2
  • 34
  • 70
  • Unfortunately this is a webforms project. If you have any sample code that makes this work in webforms please let me know. – TheEdge Dec 06 '13 at 06:30
  • WebForms are even more heavily based on controls. http://msdn.microsoft.com/en-us/library/y6wb1a0e%28v=vs.100%29.aspx – Ark-kun Dec 06 '13 at 15:19