Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 3 days ago Noscript and fritz.box #dib0 http://t.co/zxHEedNf9Q
about 10 days ago Social engineering from India #dib0 http://t.co/ajjp43WkVS
about 19 days ago @unwoman Got the kickstarter Uncovered Vol. 2 today! Thank you! I love it! http://t.co/x0Tzovtq8u
about 21 days ago A query is running #dib0 http://t.co/cRZ8Dd3nVp
15 Apr 2013 Locally save attachments from Oulook using VBScript #dib0 http://t.co/l6RhWQsvFL
1 Apr 2013 Publishing Outlook calendar for use in Google calendar through http://t.co/sfh5eNxGXM #dib0 http://t.co/IzWNPlaqNA
21 Mar 2013 http://t.co/j3B0kSLGkM Really interesting article. The church of pirates. Gods preferential option for the poor in the broadest sense.
14 Mar 2013 Happy Pi-day! And this is what's wrong with it... funny, but true. http://t.co/A8GIB8fugC
14 Mar 2013 Hey guys @piwik ! Just looking at my site stats. I love the new page overlay feature. Well done! :-)
3 Mar 2013 Really funny! The Burning Hearts Revolution: How Sesame Street is Undermining Biblical Values http://t.co/z8XFk5P4d3
26 Feb 2013 Recursively check and correct mp3 files in Linux #dib0 http://t.co/U3nzOuWzWM
26 Feb 2013 Haha! Met zo'n antwoord een terechte reactie! http://t.co/NYXIb27aP5 via @snippers
20 Feb 2013 Create random password with C#, Java and PHP #dib0 http://t.co/WgF7DtcT
Home Architecture, security and coding Using REDIPS.drag to add drag and drop to your .Net webapplication
Using REDIPS.drag to add drag and drop to your .Net webapplication
Written by Division by Zero   
Saturday, 19 May 2012 21:09

The REDIPS.drag is a free Javascript drag and drop library. I't easy to use and flexible. I love it. Here is an example how to integrate this library to you .Net webapplication. The redips site and the library download provides a lot of examples on how to work with this library. This example will focus on using it in a .Net page.

At first we will setup the page. In the header we will include the library and our own configuration script (dragdrop.js):

    <script type="text/javascript" src="/js/redips-drag-min.js"></script>
    <script type="text/javascript" src="/js/dragdrop.js"></script>

Then you can add the div's (that will be draggable, see the examples coming with the library) to the page. I prefer to add an UpdatePanel around it to make the drag and drop actions and the postback that will follow it seem fluent. The content of the page is copied from one of the examples. At the end a hidden field is added (Id is DragAction). This will be used to contain the information on which element is dropped on which location.

<asp:UpdatePanel ID="upPage" UpdateMode="Conditional" runat="server">
<ContentTemplate>
    <div>
            <div id="drag">
                <div id="draggableContent">
                    <table id="tblPage" clientidmode="Static" runat="server">
                        <tbody>
                            <tr>
                                <td id="Cell_1_1" clientidmode="Static" runat="server"></td>
                                <td id="Cell_1_2" clientidmode="Static" runat="server"></td>
                                <td id="Cell_1_3" clientidmode="Static" runat="server"></td>
                                <td id="Cell_1_4" clientidmode="Static" runat="server" class="upper_right"></td>
                            </tr>
                            <tr>
                                <td id="Cell_2_1" clientidmode="Static" runat="server"></td>
                                <td id="Cell_2_2" clientidmode="Static" runat="server"></td>
                                <td id="Cell_2_3" clientidmode="Static" runat="server"></td>
                                <td id="Cell_2_4" clientidmode="Static" runat="server"></td>
                            </tr>
                            <tr>
                                <td id="Cell_3_1" clientidmode="Static" runat="server"></td>
                                <td id="Cell_3_2" clientidmode="Static" runat="server"></td>
                                <td id="Cell_3_3" clientidmode="Static" runat="server"></td>
                                <td id="Cell_3_4" clientidmode="Static" runat="server"></td>
                            </tr>
                            <tr>
                                <td id="Cell_4_1" clientidmode="Static" runat="server" class="lower_left"></td>
                                <td id="Cell_4_2" clientidmode="Static" runat="server"></td>
                                <td id="Cell_4_3" clientidmode="Static" runat="server"></td>
                                <td id="Cell_4_4" clientidmode="Static" runat="server"></td>
                            </tr>
                        </tbody>
                    </table>
                </div><!-- right container -->
            </div><!-- drag container -->
        </div>
        <asp:HiddenField ID="dragAction" ClientIDMode="Static" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>

In the DragDrop.js we will setup de drag and drop actions. Here we will handle the item dropped event and add the information to the dragAction hidden field. Then it will submit the form (thus perform a postback).

// define redips_init variable
var redips_init;
// redips initialization
redips_init = function () {
    // reference to the REDIPS.drag
    var rd = REDIPS.drag;
    // initialization
    rd.init();
    // set hover color
    rd.hover.color_td = '#9BB3DA';
    // single element per cell
    rd.drop_option = 'single';
    // No cloning of objects
    rd.clone_shiftKey = rd.clone_shiftKey_row = false;
    // Handle dropped event
    rd.myhandler_dropped = function () {
        var obj = rd.obj,         // current element
            tac = rd.target_cell; // target cell
        var cmd = document.getElementById('dragAction');
        cmd.value = obj.id + ',' + tac.id; // Set the value to the two id's, comma seperated
        __doPostBack('upPage', ''); // Perform postback
    };
};
// add onload event listener
if (window.addEventListener) {
    window.addEventListener('load', redips_init, false);
}
else if (window.attachEvent) {
    window.attachEvent('onload', redips_init);
}

So now, if you drag and drop one of the draggable elements, there will be a postback containing the information on the latest event. In the code behind you can do something like this.

if (IsPostback)
{
    // Get the right Id's                
    string[] action = dragAction.Value.Split(',');
    int componentId = int.Parse(action[0].Split('_')[1]);
    int locationX, locationY;
    string[] location = action[1].Split('_');
    int.TryParse(location[1], locationX);
    int.TryParse(location[2], locationY);
    
    // TODO: Do what you need to do with this information
}

Of course you can build the page in the aspx file or from the codebehind. It will help you to simply create user friendly drag and drop actions.

 

Comments  

 
0 # Giz 2012-08-12 05:19
Thank you. Excellent insight into how this works. I am having trouble getting this to work properly - is it possible to post/send me a demo of how this works so I can see how everything fits together. It would be much appreciated. I have already spent about 6 hours trying to get it to work properly so frustrating.

Thank you.

Giz
Reply | Reply with quote | Quote
 
 
0 # Bas 2012-08-12 14:16
Hi Giz,

Thanks for the compliment. I've uploaded a sample solution with the code as described here. I hope it will help you getting your code working!

Download the solution here:
dib0.nl/.../REDIPS_Example.zip

Bas
Reply | Reply with quote | Quote
 
 
0 # Giz 2012-08-15 04:50
Hi Bas

Thank you so much, you are an absolute legend - the code worked brilliantly and made the whole thing make sense.

Thanks again much appreciated.

Giz
Reply | Reply with quote | Quote
 
 
0 # Bas 2012-08-15 15:45
Hi Giz,

You're welcome! Glad I could help! :D

Bas
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

If the human brain was simple enough for us to understand we'd be so simple we couldn't understand. - Unknown


© 2009 - 2013, Division by Zero

Template based on the empire template by joomlashack 

 Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.