.netasp.netvb.netlistviewedititemtemplate

How to add a new item at the top of a ListView with EditItemTemplate


I have a bit of code where you can add a new field using an add button, which enable the asp:EditItemTemplate, but the thing is that this adds the field at the bottom of the list, making the user have to scroll down if there is enough items already in the ListView. How can I make the new item appear on top of the list?

ASP.NET CODE

<asp:ListView ID="[...]" runat="server" DataSource=<% [...] %> >

    <LayoutTemplate>
        [...]
    </LayoutTemplate>

    <ItemTemplate>
        [...]
    </ItemTemplate>

    <EditItemTemplate>
        <tr>
                <td>
                    <asp:LinkButton id="btnUpdate" visible=<%# [...] %> runat="server" CommandName="Update"><%#res.GetString("btnUpdate")%></asp:LinkButton> 
                    <asp:LinkButton id="btnCancel" runat="server" CommandName="Cancel"><%#res.GetString("bntCancel")%></asp:LinkButton>

                </td>
                <td>
                    <asp:HiddenField ID="[...]" runat="server" Value='<%# [...] %>' />
                    <asp:TextBox ID="txtDisplayName" runat="server" Text=<%# Bind("DisplayName") %> Enabled=<%# [...] %> />
                </td>
        </tr>
    </EditItemTemplate>
</asp:ListView>

Solution

  • I have found the solution, you have to create an InsertItemTemplate like this:

    <asp:ListView ID="[...]" runat="server" DataSource=<% [...] %> >
        <LayoutTemplate>
            [...]
        </LayoutTemplate>
    
        <ItemTemplate>
            [...]
        </ItemTemplate>
    
        <EditItemTemplate>
            [...]
        </EditItemTemplate>
    
        <InsertItemTemplate>
        <tr>
                        <td>
                            <asp:LinkButton id="btnUpdate" visible=<%# [...] %> runat="server" CommandName="Update"><%#res.GetString("btnUpdate")%></asp:LinkButton> 
                            <asp:LinkButton id="btnCancel" runat="server" CommandName="Cancel"><%#res.GetString("bntCancel")%></asp:LinkButton>
    
                        </td>
                        <td>
                            <asp:HiddenField ID="[...]" runat="server" Value='<%# [...] %>' />
                            <asp:TextBox ID="txtDisplayName" runat="server" Text=<%# Bind("DisplayName") %> Enabled=<%# [...] %> />
                        </td>
                </tr>
        </InsertItemTemplate>
    </asp:ListView>
    

    then you have to go to the properties window of your ListView, set the InsertItemPosition to FirstItem.