Drag & Drop Panels using jQuery

Drag & Drop Panels using jQuery

Few days ago I found new theme on BBC World News Home webpage. The home page contains very new idea and this attracted me. I further google it and found they used jQuery for this purpose. The idea was implemented on home page, dragging and dropping.

After one hour googling I found some jQuery libraries on Google.com about this. Details are given below.

.column{

width:49%;

margin-right:.5%;

min-height:300px;

background:#fff;

float:left;

}

.column .dragbox{

margin:5px 2px  20px;

background:#fff;

position:relative;

border:1px solid #ddd;

-moz-border-radius:5px;

-webkit-border-radius:5px;

}

.column .dragbox h2{

margin:0;

font-size:12px;

padding:5px;

background:#f0f0f0;

color:#000;

border-bottom:1px solid #eee;

font-family:Verdana;

cursor:move;

}

.dragbox-content{

background:#fff;

min-height:100px; margin:5px;

font-family:’Lucida Grande’, Verdana; font-size:0.8em; line-height:1.5em;

}

.column  .placeholder{

background: #f0f0f0;

border:1px dashed #ddd;

}

.dragbox h2.collapse{

background:#f0f0f0 url(‘collapse.png’) no-repeat top right;

}

.dragbox h2 .configure{

font-size:11px; font-weight:normal;

margin-right:30px; float:right;

}

  • Add following code after <script> </script>
  • <link rel=”stylesheet” href=”styles.css” />
  • After that add code

<script type=”text/javascript” >

$(function(){

$(‘.dragbox’)

.each(function(){

$(this).hover(function(){

$(this).find(‘h2′).addClass(‘collapse’);

}, function(){

$(this).find(‘h2′).removeClass(‘collapse’);

})

.find(‘h2′).hover(function(){

$(this).find(‘.configure’).css(‘visibility’, ‘visible’);

}, function(){

$(this).find(‘.configure’).css(‘visibility’, ‘hidden’);

})

.click(function(){

$(this).siblings(‘.dragbox-content’).toggle();

})

.end()

.find(‘.configure’).css(‘visibility’, ‘hidden’);

});

$(‘.column’).sortable({

connectWith: ‘.column’,

handle: ‘h2′,

cursor: ‘move’,

placeholder: ‘placeholder’,

forcePlaceholderSize: true,

opacity: 0.4,

stop: function(event, ui){

$(ui.item).find(‘h2′).click();

var sortorder=”;

$(‘.column’).each(function(){

var itemorder=$(this).sortable(‘toArray’);

var columnId=$(this).attr(‘id’);

sortorder+=columnId+’='+itemorder.toString()+’&’;

});

alert(‘SortOrder: ‘+sortorder);

/*Pass sortorder variable to server using ajax to save state*/

}

})

.disableSelection();

});

</script>

  • Note all code should be add before </head>
  • Now add following code in <body>

<h3>Drag n Drop Panels</h3>

<div id=”column1″>

<div class=”dragbox” >

<h2>Heading1</h2>

<div class=”dragbox-content” >this is testing this is testing this is testing this is testing this is testing this is testing this is testing

this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing </div>

</div>

<div class=”dragbox” >

<h2>Heading2</h2>

<div class=”dragbox-content” >

this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing

</div>

</div>

<div class=”dragbox” >

<h2>Heading3</h2>

<div class=”dragbox-content” >

this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing

</div>

</div>

</div>

<div id=”column2″ >

<div class=”dragbox” >

<h2>Heading4</h2>

<div class=”dragbox-content” >

this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing

</div>

</div>

<div class=”dragbox” >

<h2>Heading5</h2>

<div class=”dragbox-content” >

this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing this is testing

</div>

</div>

</div>

<hr style=”clear:both;” />

<p>&nbsp;</p>

Multiple Files uploading Using jQuery in ASP.net

Many times developers want to get rid off from file uploading. Actually in many scenarios they have to upload multiple files at once, also they have to check file types during upload. This checking limits to end users from uploading wrong desired files to upload.

There are many solutions available in ASP.Net, jQuery, AJAX and many other client side scripting languages. In my opinion, jQuery does well above mentioned scenario.

To use jQuery multiple file upload along with file checking and number of files to upload at once, I briefly shown example below.

  • First of all download files from http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Download
  • Extract all files and into root directory
  • Open Visual Studio
  • Create new website
  • Add new webpage
  • Add following code in header section of .aspx page
  • <script type=”text/javascript” src=”Scripts/jquery-1.3.2.js”></script>
  • <script src=”Scripts/jquery.MultiFile.js” type=”text/javascript”></script>
  • Add form tag like
  • <form runat=server id=”form1″ encType=”multipart/form-data”>
  • Add file upload control, code will be like
  • <asp:FileUpload ID=”fu1″ runat=”server” Width=”250px” accept=”gif|jpg” maxlength=”1″/>
  • Add button like
  • <asp:Button ID=”btn_upload” runat=”server” OnClick=”btn_upload_Click” Text=”Upload Picture” />

Now come to function behind btn_upload_Click

Add following code in button event

string path = “Pics”;

if (fu1.PostedFile != null)

{

HttpFileCollection hfc = Request.Files;

for (int i = 0; i < hfc.Count; i++)

{

HttpPostedFile hpf = hfc[i];

if (hpf.ContentLength > 0)

{

if (File.Exists(path) == true)

{

try

{

System.IO.File.Delete(“path to physical file “);

catch (Exception r)

{}

}

}

else

{

hpf.SaveAs(Server.MapPath(path));

}

}

}

}

Now come to description of the jQuery file Upload

accept=”gif|jpg” maxlength=”3″, accept character restricts end users to select only defined extionsions of files and maxlength defines how much files will be uploaded at once.