使用中继器排序后返回列表

发布于 2024-12-24 20:51:54 字数 1414 浏览 1 评论 0原文

我有一个列表进入中继器,使用 jquery sortable() 进行排序,然后需要将排序后的列表放入会话变量中。我似乎无法弄清楚如何将排序后的值放回到列表中。

Html 代码:

<div>
<asp:Repeater ID="LstSortable" runat="server">
    <HeaderTemplate>
        <ul id="sortable">
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <%# Container.DataItem %>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

</div>

页面加载:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'create test array
    Dim order As New List(Of String)
    order.Add("Item1")
    order.Add("Item2")
    order.Add("Item3")
    order.Add("Item4")

    'testing repeater
    LstSortable.DataSource = order

    LstSortable.DataBind()

End Sub

按下按钮将排序列表放入会话变量中:

这是我正在使用的代码,它当前不起作用:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim orderBy As New List(Of String)

    Dim count As Integer
    For count = 0 To LstSortable.Items.Count - 1 Step count + 1
        Dim chk As ListItem = CType(LstSortable.Items(count).FindControl(LstSortable.ID), ListItem)
    Next

    Session("OrderBy") = orderBy

End Sub

感谢任何帮助。谢谢。

这是使用vb.net。

I have a list going into a repeater, sorting with jquery sortable(), and then need to put the sorted list into a session variable. I cannot seem to figure out how to get the sorted values back into a list.

Html code:

<div>
<asp:Repeater ID="LstSortable" runat="server">
    <HeaderTemplate>
        <ul id="sortable">
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <%# Container.DataItem %>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

</div>

page load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'create test array
    Dim order As New List(Of String)
    order.Add("Item1")
    order.Add("Item2")
    order.Add("Item3")
    order.Add("Item4")

    'testing repeater
    LstSortable.DataSource = order

    LstSortable.DataBind()

End Sub

button press to put sorted list into session variable:

This is the code I am playing with, it currently does not work:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim orderBy As New List(Of String)

    Dim count As Integer
    For count = 0 To LstSortable.Items.Count - 1 Step count + 1
        Dim chk As ListItem = CType(LstSortable.Items(count).FindControl(LstSortable.ID), ListItem)
    Next

    Session("OrderBy") = orderBy

End Sub

Any help is appreciated. Thanks.

This is using vb.net.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-12-31 20:51:54

我通过使用 jquery 创建 li 元素数组,然后使用 ajax 调用将它们传递到 Web 服务来修复此问题。

我必须在 Web 服务中启用会话:

<WebMethod(EnableSession:=True)> _

然后我将数组解析为字符串并放入会话变量中。

jquery:

        $("#sortable").sortable({
            placeholder: "vacant",
            update: function(e, ui) {

                //create vars  
                var orderArray = [], wrap = {};

                $("#sortable li").each(function(i) {

                    var imgObj = $(this).text();
                    //alert(imgObj);
                    //add object to array  
                    orderArray.push(imgObj);
                });

                //wrap in object  
                wrap.d = orderArray;

                //pass to server  
                $.ajax({
                    type: "POST",
                    url: "../WebService.asmx/updateOrder",
                    data: JSON.stringify(wrap),
                    contentType: "application/json; charset=utf-8",
                    success: function(data) {
                        if (data.d === "saved") {
                            alert("success");
                        } else {
                            alert("fail");
                        }
                    },
                    error: function(xmlHttpRequest, status, err) {
                        alert("This has error: " + err);
                    }

                });

            }
        });

网络服务:

Public Class WebService
Inherits System.Web.Services.WebService

<WebMethod(EnableSession:=True)> _
Public Function updateOrder(ByVal d As String()) As String

    'process JSON object  
    Dim orderBy As String
    Dim str As String
    orderBy = New String(" Order By ")
    For Each str In d
        'define procedure  
        orderBy += str
    Next

    Session("OrderBy") = orderBy

    'success!  
    Return "saved"

End Function

End Class

I fixed this by creating an array of li elements using jquery and then passing them to a webservice using an ajax call.

I had to enable sessions in the web service:

<WebMethod(EnableSession:=True)> _

Then I parsed the array into a string and placed in the session variable.

jquery:

        $("#sortable").sortable({
            placeholder: "vacant",
            update: function(e, ui) {

                //create vars  
                var orderArray = [], wrap = {};

                $("#sortable li").each(function(i) {

                    var imgObj = $(this).text();
                    //alert(imgObj);
                    //add object to array  
                    orderArray.push(imgObj);
                });

                //wrap in object  
                wrap.d = orderArray;

                //pass to server  
                $.ajax({
                    type: "POST",
                    url: "../WebService.asmx/updateOrder",
                    data: JSON.stringify(wrap),
                    contentType: "application/json; charset=utf-8",
                    success: function(data) {
                        if (data.d === "saved") {
                            alert("success");
                        } else {
                            alert("fail");
                        }
                    },
                    error: function(xmlHttpRequest, status, err) {
                        alert("This has error: " + err);
                    }

                });

            }
        });

web service:

Public Class WebService
Inherits System.Web.Services.WebService

<WebMethod(EnableSession:=True)> _
Public Function updateOrder(ByVal d As String()) As String

    'process JSON object  
    Dim orderBy As String
    Dim str As String
    orderBy = New String(" Order By ")
    For Each str In d
        'define procedure  
        orderBy += str
    Next

    Session("OrderBy") = orderBy

    'success!  
    Return "saved"

End Function

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文