Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Make Datalist Items Automatically Adjustable like div using JQuery and CSS

0.00/5 (No votes)
13 Mar 2009 1  
How to make datalist items automatically adjustable like div using JQuery and CSS

Introduction

This article explains how to make the datalist items automatically adjustable like div elements in HTML using JQuery and CSS as shown in the following image:   

first.JPG

Using the Code  

In div, when you give style float:left and give a width, then the div will adjust automatically into the container whenever you remove any in between div.

When you are using datalist to display image listing and using JQuery to delete any image, then if you delete any in between image then the page will not get refreshed and the image will be deleted but it will display the empty space there as follows. You can see the empty space in the second line right most in the following image.

second.JPG

The reason behind this is that the default value for the 'RepeatLayout' property is 'Table'. It means the items are in table and tr, td layout. So it can't move automatically like div or span elements.

Now let us talk about the solution:

  1. Make the 'RepeatLayout' property of datalist 'Flow' as in the following example:
    • Example: 
      RepeatLayout="Flow"  
    • By this property, the datalist item will be converted into span elements rather than tr, td elements.
    • And also remove RepeatColumns property
    • HTML tag:  
    • <asp:DataList ID="dlPhotos" runat="server" 
      DataSourceID="sqlListing" RepeatDirection="Vertical" RepeatLayout="Flow"> 
  2. Add class to datalist for hide tags like:
    .hidebr br
    {
    display:none; 
    }  
    • The reason to add this class is to make structure proper aligned, means each and every span is aligned to each other. If you don't add this class, then the structure will be shown like the following:

      third.JPG

    • Now the HTML tag will be like:
      <asp:DataList ID="dlPhotos" runat="server" DataSourceID="sqlListing" 
      RepeatDirection="Vertical" RepeatLayout="Flow" CssClass="hidebr">
  3. Now give any class name to an ItemStyle tag of the datalist:
    • Example:
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" 
      	CssClass="tdContainer"/>
    • So it applies class tdContainer to all the span elements.
  4. And then write the JQuery code as follows:
  5. First, please add latest JQuery file in your page. You can download it from here OR here.

    $(document).ready(function(){
    var tdImg = $("#<%=dlPhotos.ClientID%> .tdContainer");
    tdImg.attr("style",'width:150px;float:left;');
    )};     
    • So it will give width of 150px to all the span elements and also give float:left property, So now when you delete any in between image then all the span elements will align automatically.    

Do the above four steps and enjoy......:-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here