asp.net ajax like reorder list

感情迁移 提交于 2019-12-24 08:14:05

问题


I'm trying to create a draggable list to change the order some pictures are displayed in a product page. I wan't to be able to do this in the product edit page (same place where I add the pics and their description). So, when creating I have nothing inserted on the database and since the AJAX toolkit reorderlist only works with a datasource I was specifying the list as the source of the reorderlist and calling the databind method. In the item template of the reorder list I have a textbox to edit the pic description and a img that displays the photo. I can drag the items and the list gets reordered, however when I click save I can't get the updated text on the textbox and the order property on the picture doesn't get updated. I've tried manually getting the items in the reorderlist but they're always null even though the list shows 20 items the DataItem is empty. I've enabled viewstate and didn't help either.

Here's my code:

<ajaxToolkit:ReorderList ID="rdlPhotos" runat="server" SortOrderField="PhotoOrder" AllowReorder="true" PostBackOnReorder="true" ClientIDMode="AutoID" EnableViewState="true" ViewStateMode="Enabled">
            <ItemTemplate>
                <div>
                <%--<eva:PhotoView ID="iPV" runat="server" Photo='<%# Container.DataItem %>' />--%>
                    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# string.Format("http://eva-atelier.net/sparkle{0}", Eval("Path").ToString().Substring(1)) %>' Width="150" Height="150" />
                    <div class="formGrid">
                        <label class="formLabel">Title</label>
                        <asp:TextBox ID="txtTitle" runat="server" Text='<%#Bind("FileTitle") %>' />
                        <br />
                        <label class="formLabel">Description</label>
                        <asp:TextBox ID="txtDescription" runat="server" Text='<%#Bind("FileDescription") %>' />
                        <br />
                    </div>
                    <p>
                        <asp:Button ID="btnRemove" runat="server" Text="Remover" />
                    </p>
                </div>
            </ItemTemplate>
            <ReorderTemplate>
                <asp:Panel ID="pnlReorder" runat="server" />
            </ReorderTemplate>
            <DragHandleTemplate>
                <div style="width:20px;height:20px;background-color:Red"></div>
            </DragHandleTemplate>
        </ajaxToolkit:ReorderList>

And below the C# code:

private void AddPhotosView()
        {
            if (_currentProduct.Photos != null && _currentProduct.Photos.Count > 0)
            {
                rdlPhotos.DataSource = _currentProduct.Photos;
                rdlPhotos.DataBind();
            }
        }

I'm new to Asp.net I come from a large WPF background, sorry if I'm making basic question :)

Thanks


回答1:


For updating order of ReorderList items add your handler for it's OnItemReorder event. In this case your handler may looks like this:

protected void ReorderHandler(object sender, ReorderListItemReorderEventArgs  e)
{
    var movedPhoto = _currentProduct.Photos[e.OldIndex];
    _currentProduct.Photos.RemoveAt(e.OldIndex);
    _currentProduct.Photos.Insert(e.NewIndex, movedPhoto);
    _currentProduct.Photos.Save();
}

For updating FileTitle and FileDescription of single Photo it is easy to use OnUpdateCommand event of ReorderList and a button with attribute CommandName="Update" for each Photo. And for updating all Photos at once just iterate through rdlPhotos.Items in next manner:

protected void SaveAllHandler(object sender, EventArgs e)
{
    foreach (var riItem in rdlPhotos.Items)
    {
        var id = ((HiddenField)riItem.FindControl("itemID")).Value;
        var title = ((TextBox)riItem.FindControl("txtTitle")).Text;
        var description = ((TextBox)riItem.FindControl("txtDescription")).Text;

        UpdatePhoto(id, title, description);
    }
}

Remember that rdlPhotos.Items here are in initial order. And for identifying which Photo should be updated add hidden field with Photo.ID-value to ReorderList's ItemTemplate like this:

<asp:HiddenField runat="server" ID="itemID" Value='<%# Eval("ID") %>' />


来源:https://stackoverflow.com/questions/7454972/asp-net-ajax-like-reorder-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!