Tuesday, March 16, 2010

Dynamic Round Cornered Tab Menu without using images

Tab menus are one of the most common form of navigation buttons used in web design, the most common of which is usually a round cornered tab. To make round cornered tabs, you will usually have to use images for the corners/edges to get the effect. I have implemented a dynamic Tab menu using jQuery and CSS without using images. Taking the concept from my previous post on Round Cornered Div tags without using images, I have implemented this round cornered tab menu using the nifty power of jQuery which makes it dynamic in nature.

Why should you use this Tab Menu:

  1. VERY easy to use and modify.
  2. Easy to understand and modify the CSS.
  3. Faster to load because no images are used.
  4. The changeTab() function can change the tab selection dynamically.
  5. This Tab Menu can be used with Ajax like functionalities as well where the contents can be loaded dynamically based upon the click event.
  6. Can be used with your dynamic code just by setting the <li> class to ‘sel’ using your code.
  7. Scalable to any size by modifying the CSS only.

This is how the tabbed menu looks like:image

How does it work: The trick is pretty simple; The header for every tab is set dynamically by using jQuery. When a tab selection is made, the inactive tab header is replaced by the active tab header and vice versa.

The JavaScript/jQuery code:
The jQuery code is pretty simple to understand because only selectors are used to select the appropriate tab and change the CSS styles and colors. When the page loads for the first time, rounded corners and CSS is applied to the li tag with class ‘sel’ and a click event is assigned to all the p or a tags inside the Tab Menu.
    The changeTab() function is used to handle the tab click event for swapping the active and inactive tabs. Along with that any kind of Ajax calls or page redirects can be made based upon the tab button clicked. The TabRoundedCorners() function is used to populate the Tab header based upon active or inactive type.

Put the following code in the head section of your page:
/* Developed by: Abhinay Rathore [web3o.blogspot.com] */
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
      //Add Inactive tab round corners where class not equals to 'sel'
      $("#tabs > ul > li[class!='sel']").prepend(TabRoundedCorners('inactive'));
      //Add Active tab round corners where class equals to 'sel'
      $("#tabs > ul > li[class='sel']").prepend(TabRoundedCorners('active'));
      //Select tab where li class equal to 'sel'
      $("#tabs > ul > li[class='sel'] > p").addClass('sel');
      $("#tabs > ul > li[class='sel'] > a").addClass('sel');
      //Assign click event to all the tabs
      $("#tabs > ul > li > p").click(function(){ changeTab(this) });
      $("#tabs > ul > li > a").click(function(){ changeTab(this) });
     
});

function changeTab(tabId)
{
      //Remove Active tab round corners where class equals to 'sel'
      $("#tabs > ul > li[class='sel']").children(":first-child").remove();
      //Add Inactive tab round corners where class equals to 'sel'
      $("#tabs > ul > li[class='sel']").prepend(TabRoundedCorners('inactive'));
      //Remove 'sel' class for the currently selected tab
      $("#tabs > ul > li[class='sel']").children().removeClass('sel');
      $("#tabs > ul > li[class='sel']").removeClass('sel');
      //Remove Inactive rounded corners from clicked tab
      $(tabId).parent().children(":first-child").remove();
      //Add 'sel' class to p tag under clicked tab
      $(tabId).parent().children(":first-child").addClass('sel');
      //Add 'sel' class to the clicked tab li
      $(tabId).parent().addClass('sel');
      //Add Active tab round corners to the clicked tab
      $(tabId).parent().prepend(TabRoundedCorners('active'));
     
      // Perform Action based upon the selected tab button
      // if you are using Ajax to change your page content
      switch($(tabId).text())
      {
            case "Home": break;
            case "Work": break;
            case "Personal": break;
            case "Blog": break;
            case "About": break;
            case "Contact": break;
      }
}

function TabRoundedCorners(type){
      return "<div id=\'" + type + "_tab_rounder\'>" +
            "<div class=\'top_left\'><div class=\'top_left_inside\'>&#8226;</div></div>" +
            "<div class=\'top_right\'><div class=\'top_right_inside\'>&#8226;</div></div>" +
            "</div>";
}
</script>

CSS code:
The CSS code contains very minimal amount of CSS for the tabs and the round cornered tab headers. There are two sets of round cornered header CSS for active and inactive tabs. To make any modifications in the size of the tabs should be done to both these sets for consistency. To change the Tab colors, you only need to replace LightBlue and Gainsboro with your custom colors values in the whole CSS section.

Place the following CSS code in the head section of your page:
<style type="text/css">
<!--
#tabs {
      margin: 0px;
      padding: 0px;
}
#tabs ul
{
      margin: 0px;
      padding: 0px;
      list-style: none;
}
#tabs li
{
      margin: 0px 1px 0px 1px;
      float: left;
      padding: 0px;
      /*Set width to 'auto' for automatic width*/
      width: 80px;
}
#tabs p, #tabs a
{
      display: block;
      color: #069;
      font-family: Verdana, Geneva, sans-serif;
      font-size: small;
      text-decoration: none;
      background-color: Gainsboro;
      padding: 0px 8px 4px 8px;
      margin: 0px;
      cursor: pointer;
      text-align: center;
}
#tabs p:hover, #tabs a:hover
{
      color: #FFF;
}
#tabs p.sel, #tabs a.sel
{
      color: #FFF;
      background-color: LightBlue;
      font-weight: bold;
}

/* Active Tab rounded corners */

#active_tab_rounder { height: 5px; position: relative; background-color: LightBlue;}
#active_tab_rounder .top_left, #active_tab_rounder .top_right {
      height: 5px; width: 5px;
      position: relative; overflow: hidden;
        background-color: White;
}
#active_tab_rounder .top_left { float: left; }
#active_tab_rounder .top_right { float: right; }
#active_tab_rounder .top_left_inside, #active_tab_rounder .top_right_inside {
      height: 5px; width: 5px;
      font-family: Arial, Helvetica, sans-serif;
      font-size: 36px; color: LightBlue;
      position: relative; line-height: 10px;
}
#active_tab_rounder .top_left_inside { left: -2px; }
#active_tab_rounder .top_right_inside { left: -6px; }

/* Inactive Tab rounded corners */
#inactive_tab_rounder { height: 5px; position: relative; background-color: Gainsboro;}
#inactive_tab_rounder .top_left, #inactive_tab_rounder .top_right {
      height: 5px; width: 5px;
      position: relative; overflow: hidden;
        background-color: White;
}
#inactive_tab_rounder .top_left { float: left; }
#inactive_tab_rounder .top_right { float: right; }
#inactive_tab_rounder .top_left_inside, #inactive_tab_rounder .top_right_inside {
      height: 5px; width: 5px;
      font-family: Arial, Helvetica, sans-serif;
      font-size: 36px; color: Gainsboro;
      position: relative; line-height: 10px;
}
#inactive_tab_rounder .top_left_inside { left: -2px; }
#inactive_tab_rounder .top_right_inside { left: -6px; }
-->
</style>

HTML code:
The HTML code for the Tab Menu is very simple and easy to modify. The Tab Menu is represented by an unordered HTML list. The list elements can contain HTML links or Paragraph tags for tab heading. The default selected tab can be set by assigning the ‘sel’ class to the li tag.

Modify and place the following code inside the your HTML page body:
<div id="tabs">
<ul>
    <li><a href="javascript:void(0)">Home</a></li>
    <li><p>Work</p></li>
    <li class="sel"><p>Personal</p></li>
    <li><p>Blog</p></li>
    <li><p>About</p></li>
    <li><p>Contact</p></li>
</ul>
</div>

Friday, March 12, 2010

Animated Page Scrolling with jQuery

Many a times we use HTML links to link to some section on the same page and we link them using anchor tags. Using the nifty power of jQuery you can animate this effect by scrolling the page slowly when clicked on the anchor links. I have devised this easy to use function which can be used to scroll the HTML page/window to any HTML element on the page.

The ScrollTo function requires the id of the HTML element you want to scroll to and the duration for how long the animation should run. Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively. You can even call this function without the duration in which case the default duration is set to 400ms.

Just include the following JavaScript code in the head section of your HTML page:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function ScrollTo(id, duration)
{
      if(!duration) duration = 400;
      var offset = $('#' + id).offset().top;
      $('html,body').animate({scrollTop: offset}, duration);
}
</script>

You can call this ScrollTo function from any DOM event like mouse clicks or key press. It can be used with any HTML element. Just make sure the id you pass to the function should be an unique id of any HTML element on the page.

HTML Examples:
<a href="javascript:void(0)" onclick="ScrollTo('placeToGo', 500)">Go To Link</a>
<a href="javascript:void(0)" onclick="ScrollTo('placeToGo')">Go To Link</a>
<img src="ClickButton.gif" onclick="ScrollTo('placetoGo') />

Thursday, November 19, 2009

Draw rotated text in VB/ASP.NET using the Matrix class

While working on 2D drawings in VB.NET, you might find yourself in a situation where you may want to rotate a text or any other object on the drawing canvas. I discovered this technique when I was trying to draw my text vertically from bottom to top and the System.Drawing.StringFormat class’s built in vertical text format would only support top to bottom layout. Then I discovered the System.Drawing.Drawing2D.Matrix class which is used for geometric transformation. Using the Matrix class you can rotate the drawing canvas to any desired angle, draw the text/object and then rotate it back to the original position.

Example of rotated text:
Rotated text in VB .NET

I have implemented the drawing by using ASP.NET web application where the drawing is generated by an aspx page. To view the image in web browser, you just need to create an HTML image tag on a new page and set the image source(src) to the .aspx file. The .aspx file code to generate a sample PNG file is given below. The main functionality for rotating the text is implemented in the RotateString subroutine which is provided with the text, rotation angle, and the x and y coordinates.

Imports System.Drawing
Imports System.IO
Imports System.Drawing.Drawing2D

Partial Public Class TextImageDraw
    Inherits System.Web.UI.Page

    'Dimensions of the bitmap
    Const DRAWING_HEIGHT As Integer = 300
    Const DRAWING_WIDTH As Integer = 300

    Dim bitmap As New Bitmap(DRAWING_WIDTH, DRAWING_HEIGHT)
    Dim myDrawing As Graphics = Graphics.FromImage(bitmap)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myDrawingBackgroundColor As New SolidBrush(Color.AliceBlue)
        'Fill Drawing Background
        myDrawing.FillRectangle(myDrawingBackgroundColor, 0, 0, DRAWING_WIDTH, DRAWING_HEIGHT)

        myDrawing.TextRenderingHint = Text.TextRenderingHint.AntiAlias

        Dim myText As String = "   web3o.blogspot.com"

        'Text Rotation Cordinates
        Dim xCordinate As Integer = DRAWING_WIDTH / 2
        Dim yCordinate As Integer = DRAWING_HEIGHT / 2

        'Draw Rotation Center
        myDrawing.DrawEllipse(Pens.Black, New Rectangle(xCordinate - 1, yCordinate - 1, 3, 3))

        'Draw Header Text
        Dim headingText As String = "Rotating Text"
        Dim headingFont As Font = New Font("arial", FontSize.XLarge, FontStyle.Regular)
        Dim headingTextWidth As Double = myDrawing.MeasureString(headingText, headingFont).Width
        Dim headingTextHeight As Double = myDrawing.MeasureString(headingText, headingFont).Height
        myDrawing.DrawString(headingText, headingFont, Brushes.Red, _
                             (DRAWING_WIDTH - headingTextWidth) / 2, 2)

        'Draw Text in 360 degrees with 30 degree interval
        For angle = 0 To 330 Step 30
            RotateString(myText, angle, xCordinate, yCordinate)
        Next

        'Draw Footer Text
        myDrawing.DrawString(headingText, headingFont, Brushes.Red, _
                            (DRAWING_WIDTH - headingTextWidth) / 2, _
                            DRAWING_HEIGHT - headingTextHeight - 2)

        'Set smoothing mode for the drawing to Anti-alias
        myDrawing.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

        'Write the image to browser...
        Response.ClearContent()
        Response.ContentType = "image/png"
        Dim mem As New MemoryStream()
        bitmap.Save(mem, System.Drawing.Imaging.ImageFormat.Png)
        mem.WriteTo(Response.OutputStream)
        myDrawing.Dispose()
        bitmap.Dispose()
    End Sub

    Private Sub RotateString(ByVal Text As String, ByVal angle As Integer, _
                             ByVal x As Integer, ByVal y As Integer)
        Dim myFont As Font = New Font("arial", FontSize.XLarge, FontStyle.Regular)
        Dim myColor As SolidBrush = New SolidBrush(Color.Black)
        ' Make a rotation matrix
        Dim myMatrix As New Matrix
        myMatrix.RotateAt(angle * -1, New Point(x, y)) 'Rotate drawing
        myDrawing.Transform = myMatrix
        myDrawing.DrawString(Text, myFont, myColor, x, y) 'Draw the text string
        myMatrix.RotateAt(angle, New Point(x, y)) 'Rotate back
        myDrawing.Transform = myMatrix
    End Sub

End Class

You can just copy the RotateString subroutine to your code and pass the Graphics class object along with other parameters to draw the rotated string on it.

Multiple File Uploads in ASP.NET with Add/Remove and Up/Down functionalities

HTML or ASP.NET does not allows you to select more then one file in a single file upload input field. To work around this shortcoming you will either have to add dynamic file upload fields by using JavaScript or use a single file upload field to upload the files one by one.

I have devised a nifty technique by using the ASP.NET ListBox control to keep track of the added files and manage them. You can add/remove or change the upload order of the files before uploading them all to your server/database. Sometimes the order of your file upload is also very important and it becomes more easier with this technique where you can arrange them by using the Up/Down controls.

Multiple File Uploads in ASP.NET

The code for the .aspx file is pretty straight forward with a FileUpload control, a ListBox control and buttons to handle all the functionalities. You can even implement this code as an ASP.NET User Control for your project.

Here is the File Upload code for .aspx file:
<table>
<tr>
    <td colspan="2">
    <font color="red"><asp:Label ID="Message" runat="server" Text=""></asp:Label></font>
    </td>
</tr>
<tr>
    <td colspan="2">
    <asp:FileUpload runat="server" id="FileUploader" Width="300"></asp:FileUpload>
    </td>
</tr>
<tr>
    <td>
    <asp:ListBox ID="FileListBox" runat="server" Width="300" Rows="5" SelectionMode="Single" BackColor="AliceBlue">
    </asp:ListBox>
    </td>
    <td>
    <asp:Button ID="Up" runat="server" Text="&uarr;" Font-Bold="true" Font-Size="Large" /><br />
    <asp:Button ID="Down" runat="server" Text="&darr;" Font-Bold="true" Font-Size="Large" /><br />
    </td>
</tr>
<tr>
    <td colspan="2">
    <asp:Button ID="Add" runat="server" Text="Add" />
    <asp:Button ID="Remove" runat="server" Text="Remove" />
    <asp:Button ID="Upload" runat="server" Text="Upload" />
    </td>
</tr>
</table>
<br /><br />
<asp:Literal ID="FileList" runat="server"></asp:Literal>

The backend code in VB is also pretty easy to understand with subroutines to handle the button click events. To keep track of all the files added to the list we use a Listbox control and to store the HttpInputFile objects we use a global ArrayList named Files. For saving the files I have used an example of storing the files in a Database using stored procedure, but you can always save the file to your server itself by using the HIF.PostedFile.SaveAs method. Detailed description for the functionality of each button click event subroutine is given below:

  • Add_Click: Fires when the user browses a file and click on the Add button. Error checking is done to ensure if the FileUpload control has input file, if the file size is not zero and does the file already exists in the list or not. After error checking is passed, the file name is added to to List Box and also to the global ArrayList which holds the object to the HttpInputFile class.
  • Remove_Click: This subroutine simply removes the file name at selected index from the List Box and from the Files Array.
  • Up_Click: The position of the selected item is switched with the item on top of it in the List Box and the Array List.
  • Down_Click: The position of the selected item is switched with the item below it in the List Box and the Array List.
  • Upload_Click: All the files in the Files Array are uploaded to the database using stored procedure.

Here is the VB file code for the File Uploader:

Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public Shared Files As ArrayList = New ArrayList()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub Add_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Add.Click
        Try
            Message.Text = ""
            If FileUploader.HasFile Then 'Check if file exists...
                If FileUploader.PostedFile.ContentLength > 0 Then 'Check file size
                    If FileListBox.Items.Contains(New ListItem(System.IO.Path.GetFileName(FileUploader.PostedFile.FileName))) Then
                        Message.Text = "File already in the list!"
                    Else
                        Files.Add(FileUploader)
                        FileListBox.Items.Add(System.IO.Path.GetFileName(FileUploader.PostedFile.FileName))
                        Message.Text = "Add another file or click Upload to save them all..."
                    End If
                Else
                    Message.Text = "File size cannot be 0!"
                End If
            Else
                Message.Text = "Please select a file to add!"
            End If
        Catch ex As Exception
            ' Handle Error
        End Try
    End Sub

    Protected Sub Remove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Remove.Click
        If FileListBox.Items.Count > 0 Then
            If FileListBox.SelectedIndex < 0 Then
                Message.Text = "Please select a file to remove!"
            Else
                Files.RemoveAt(FileListBox.SelectedIndex)
                FileListBox.Items.Remove(FileListBox.SelectedItem.Text)
                Message.Text = "File removed..."
            End If
        End If
    End Sub

    Protected Sub Up_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Up.Click
        If FileListBox.Items.Count > 1 AndAlso FileListBox.SelectedIndex > 0 Then
            Dim index As Integer = FileListBox.SelectedIndex
            Dim toMove As String = FileListBox.SelectedValue
            'Move up filename in ListBox
            FileListBox.Items.Insert(index - 1, toMove)
            FileListBox.Items.RemoveAt(index + 1)
            FileListBox.Items.Item(index - 1).Selected = True
            'Move up file in Files ArrayList
            Dim TempFileHandle As FileUpload = Files(index - 1)
            Files(index - 1) = Files(index)
            Files(index) = TempFileHandle
        End If
    End Sub

    Protected Sub Down_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Down.Click
        If FileListBox.Items.Count > 1 AndAlso FileListBox.SelectedIndex < FileListBox.Items.Count - 1 Then
            Dim index As Integer = FileListBox.SelectedIndex + 1
            Dim toMove As String = FileListBox.Items.Item(index).Text
            'Move down filename in ListBox
            FileListBox.Items.Insert(index - 1, toMove)
            FileListBox.Items.RemoveAt(index + 1)
            FileListBox.Items.Item(index).Selected = True
            'Move down file in Files ArrayList
            Dim TempFileHandle As FileUpload = Files(index - 1)
            Files(index - 1) = Files(index)
            Files(index) = TempFileHandle
        End If
    End Sub

    Protected Sub Upload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Upload.Click
        Dim conn As SqlConnection = New SqlConnection("")
        conn.Open()
        For Each HIF As FileUpload In Files
            FileList.Text += "Uploading file: " & HIF.PostedFile.FileName + "<br />" + vbCrLf
            Dim fileBytes(HIF.PostedFile.InputStream.Length) As Byte
            HIF.PostedFile.InputStream.Read(fileBytes, 0, fileBytes.Length)
            Dim Command As New SqlCommand
            With Command
                .Connection = conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "upload_files"
                .Parameters.AddWithValue("@id", System.Guid.NewGuid.ToString())
                .Parameters.AddWithValue("@name", System.IO.Path.GetFileName(HIF.PostedFile.FileName))
                .Parameters.AddWithValue("@size", HIF.PostedFile.ContentLength)
                .Parameters.AddWithValue("@type", HIF.PostedFile.ContentType)
                .Parameters.AddWithValue("@file", fileBytes)
                .Parameters.AddWithValue("@dateadded", DateTime.Now())
            End With
            Command.ExecuteNonQuery()
        Next
        conn.Close()
        Message.Text = ""
        'Clear Files ArrayList
        Files.Clear()
        'Clear File List Box
        FileListBox.Items.Clear()
    End Sub
End Class

This implementation contains very minimal amount of code to attain the desired functionalities, you can always add more error checking or styles to suit your website design.

Friday, November 13, 2009

Making Checkboxes behave like Radio Buttons using jQuery

Sometimes you might need Checkboxes to behave like Radio Buttons on your webpage because you may only want zero or one selection from a category of choices. You cannot select 0 or 1 choice from a Radio Button Group because Radio Button groups are meant to select only one choice from a group.

I have written an easy to use and understand code in jQuery for achieving this task. In this example, all the checkboxes with name Colors are assigned a click function (Checkbox_to_RadioButton()) when the page loads for the first time. The Checkbox_to_RadioButton() is a generic function which will automatically uncheck all other other checkboxes with the same name/group.

Too make this work on your page, you just need to paste this JavaScript code in the head section of your webpage and set the Checkbox group name in the page ready function.

JavaScript Code:
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
      $('input:checkbox[name=Colors]').click(function(){
            Checkbox_to_RadioButton(this);
      });
});
function Checkbox_to_RadioButton(box){
      $('input:checkbox[name=' + box.name + ']').each(function(){
            if (this != box) $(this).attr('checked', false);
      });
}
</script>

HTML Code:
<input type="checkbox" name="Colors" value="Red" />Red<br />
<input type="checkbox" name="Colors" value="Blue" />Blue<br />
<input type="checkbox" name="Colors" value="Green" />Green<br />
<input type="checkbox" name="Colors" value="Yellow" />Yellow<br />