6

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

 3 years ago
source link: https://www.codesd.com/item/how-to-add-a-new-item-to-the-top-of-a-listview-with-edititemtemplate.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

advertisements

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>


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK