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=<%# [...] %> /&g开发者_高级运维t;
</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
.
Kind of a late answer for this forum but you could change your SELECT command to order the list by id in the SQL table.
Example:
SelectCommand="SELECT * FROM [Bulletin] ORDER BY [m_id] DESC;"
I built a forum style bulletin in my project and I wanted the last posts to appear at the top. That's how I ended up doing it. Of course, this assumes that your SQL table "ID" is set to auto increment.
精彩评论