简体   繁体   中英

Get list order after dragging and dropping list items asp.net c#

I've got the following code that allows drag and drop of list items:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <style>
        header, section {
            display: block;
        }
        body {
            font-family: 'Droid Serif';
        }
        h1, h2 {
            text-align: center;
            font-weight: normal;
        }
        #features {
            margin: auto;
            width: 460px;
            font-size: 0.9em;
        }
        .connected, .sortable, .exclude, .handles {
            margin: auto;
            padding: 0;
            width: 310px;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
        .sortable.grid {
            overflow: hidden;
        }
        .connected li, .sortable li, .exclude li, .handles li {
            list-style: none;
            border: 1px solid #CCC;
            background: #F6F6F6;
            font-family: "Tahoma";
            color: #1C94C4;
            margin: 5px;
            padding: 5px;
            height: 22px;
        }
        .handles span {
            cursor: move;
        }
        li.disabled {
            opacity: 0.5;
        }
        .sortable.grid li {
            line-height: 80px;
            float: left;
            width: 80px;
            height: 80px;
            text-align: center;
        }
        li.highlight {
            background: #FEE25F;
        }
        #connected {
            width: 440px;
            overflow: hidden;
            margin: auto;
        }
        .connected {
            float: left;
            width: 200px;
        }
        .connected.no2 {
            float: right;
        }
        li.sortable-placeholder {
            border: 1px dashed #CCC;
            background: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <section>
        <h2>Sortable List</h2>
        <ul class="sortable list">
            <li><asp:Label ID="lblRouting1" runat="server" Text="Routing 1"></asp:Label></li>
            <li> <asp:Label ID="lblRouting2" runat="server" Text="Routing 2"></asp:Label></li>
            <li> <asp:Label ID="lblRouting3" runat="server" Text="Routing 3"></asp:Label></li>
            <li> <asp:Label ID="lblRouting4" runat="server" Text="Routing 4"></asp:Label></li>
            <li> <asp:Label ID="lblRouting5" runat="server" Text="Routing 5"></asp:Label></li>
            <li> <asp:Label ID="lblRouting6" runat="server" Text="Routing 6"></asp:Label></li>
        </ul>
    </section>
    </div>
    </form>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="jquery.sortable.js"></script>
        <script>
            $(function () {
                $('.sortable').sortable();
                $('.handles').sortable({
                    handle: 'span'
                });
                $('.connected').sortable({
                    connectWith: '.connected'
                });
                $('.exclude').sortable({
                    items: ':not(.disabled)'
                });
            });
    </script>
</body>
</html>

It allows the user to drag the list items to the order that they require it. I'm trying to find out if there's a way in c# that you can get the order of the li when the user has changed the order please?

One way to accomplish what you want, would be to submit a change the user does, back to serverside using ajax.


----------------------
JavaScript Part:
----------------------

in your .sortable() initialization, you need to add a stop handler that will fire when you drop the dragged item. the following code alerts the new position of the dropped element:

$('.sortable').sortable({
    stop: function (event, ui) {
        alert("New position: " + ui.item.index()); 
    }
});

now that we have the new position of the dragged element, you need to submit it back to the server.
we do it by sending 2 arguments to the server:

  • a unique ID to identify the element (could be a pre-set custom attribute that you assign to the element)
  • the new position

to submit details back to the server, we will use ajax. first, we declare a var with the options for the ajax:

$(function () {
     $('.sortable').sortable({
         stop: function (event, ui) {
             var ID_To_Submit = ui.item.attr("myCustomIDAtribute");
             var New_Position = ui.item.index();
             var options = {
                 type: "POST",
                 url: "./myWebPage.aspx/myWebMethod",
                 data: JSON.stringify({
                     ID: ID_To_Submit,
                     POS: New_Position
                 }),
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (response) {

                 }
             };

             //and then, we submit the ajax with the options:
             $.ajax(options);
         }
     });
 });

explanation:

  • assuming we set a custom attribute to our <li myCustomIDAtribute="12"> element,
    this is how we retrieve it
    var ID_To_Submit = ui.item.attr("myCustomIDAtribute");

  • this will obviously get the new position:
    var New_Position = ui.item.index();

  • here you specify the path to your aspx page where the webmethod that will receive the
    ajax is located, and the name of the method:
    url: "./myWebPage.aspx/myWebMethod",

  • this one is tricky, here you specify the name of the arguments in the webmethod on server side , and what they will receive. here the argument names on server side will be ID and POS , and they will receive the value of the javascript vars ID_To_Submit and New_Position accordingly.
    data: JSON.stringify({ID:ID_To_Submit,POS:New_Position}),
    remember to stringify them using json, as we are sending it in a json string.

  • the success: function (response) {} is a callback function that fires when the server returns from the web method. usually the return value is placed directly in response argument, but in ASP.NET it is located in response.d


----------------------
C# Part:
----------------------

in your myWebPage.aspx page, in the code behind, you will create a web method that will receive ajax posts:

you will need to declare it as a [WebMethod]

[WebMethod]
public static string myWebMethod(ID,POS)
{
    //do what you need with the ID and the new POS
    if(/*everything updated fine*/)
    {
        return "changed";
    }
    else
    {
        return "failed";
    }
}
  • note: you can return whatever object you want, just parse it on javascript side correctly.


------------------------------
Back to JavaScript Part:
------------------------------

remember the ajax's success function? the return value will be placed in response.d
remember how we returned a string "changed" or "failed" ?

var options = {
     // ...
     // all the options
     //...
     success: function (response) {
         if (response.d == "changed") {
             //position updated on server successfully
         } else if (response.d == "failed") {
             //did not update on server successfully
         }
     }
 };

Thats about it. if you have any questions, feel free to ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM