Click here to Skip to main content
Click here to Skip to main content

ASP.NET Splitter Bar - Resize panels, table cells, and controls

By , 10 Mar 2008
 

Screenshot - VwdCmsSplitterBar.gif

  • Try out the SplitterBar online: SplitterBar Online Demo
  • The VwdCms.SplitterBar now supports both vertical and horizontal orientations

Introduction

The VwdCms.SplitterBar control is designed to give you column resizing in a two-column page layout. Use the splitter bar to resize a panel, div, table cell, or just about any control.

This control is ideal for pages that have a tree or a list of data items that vary in width.

The screenshots at the top of this article are an example of the VwdCms.SplitterBar in use, with a TreeView control in the left column of a table and a TextArea control in the right column. See the demo project (Example 1) for information on the TextArea sizing, scrolling, and text wrapping issue in IE 6 and IE 7, and how to overcome it.

Using the Control

Suggestion: Download the demo project rather than only the code for the control, as the demo project has many examples of how to use the control and how not to use it. The demo project has lots of descriptions and explanations of how to do things, so I won't bother repeating all of that information in this article.

To add the VwdCms.SplitterBar control to your project, just follow these steps:

Step 1: Copy these files into your project directory:

  1. /App_Code/SplitterBar.cs
  2. /VwdCmsSplitterBar.js
  3. /vsplitter.gif

Step 2: Add the VwdCmsSplitterBar.js file to the HEAD section of your Web Form:

<head runat="server">
    <title>VwdCms SplitterBar Web Form</title>
    <script src="VwdCmsSplitterBar.js" type="text/javascript"></script> 
</head>

Step 3: Update your web.config file's Controls section (inside the system.web section):

    <pages>
        <controls>
            <add tagPrefix="VwdCms" namespace="VwdCms"/> 
        </controls>
    </pages>

Step 4: Add a VwdCms.SplitterBar control to your Web Form:

    <VwdCms:SplitterBar runat="server" ID="vsbSplitter1" 
        LeftResizeTargets="tdTree1;divTree1" 
        MinWidth="100" 
        MaxWidth="700"
        BackgroundColor="lightsteelblue" 
        BackgroundColorLimit="firebrick"
        BackgroundColorHilite="steelblue"
        BackgroundColorResizing="purple"
        SaveWidthToElement="txtWidth1"
        OnResizeFunction="splitterOnResize"
        style="background-image:url(vsplitter.gif);
            background-position:center center;
            background-repeat:no-repeat;"/>

An example of the OnResizeFunction:

<script language="javascript" type="text/javascript"><!--

    function splitterOnResize(width)
    {
        // do any other work that needs to happen when the 
        // splitter resizes. this is a good place to handle 
        // any complex resizing rules, etc.

        // make sure the width is in number format
        if (typeof width == "string")
        {
            width = new Number(width.replace("px",""));
        }

        // now do something...
    }
    
// -->
</script>

An example of the table that this VwdCms.SplitterBar is connected to:

    <div style="margin:0px;padding:0px;
    width:800px:overflow:hidden;">
    <table border="0" cellpadding="0" 
        cellspacing="0" 
        style="width:800px;height:200px;
        border:solid 1px #6699CC;">
        <tr style="height:200px;">
            <td id="tdTree1" 
            style="width:250px;height:200px;" 
            align="left" valign="top"> 
              <div id="divTree1" 
                style="width:100%;height:100%;overflow:auto;
                padding:0px;margin:0px;">
                <asp:TreeView ID="tvwFramework1" 
                runat="server" ShowLines="True"
                style="width:100%;height:100%;
                padding:0px;margin:0px;">
                <Nodes>
                <asp:TreeNode Text=".NET Framework" 
                Value=".NET Framework">
                <asp:TreeNode 
                Text="System.Diagnostics" 
                Value="System.Diagnostics">
                <asp:TreeNode 
                Text="Debug Class" 
                Value="Debug"></asp:TreeNode>
                <asp:TreeNode 
                Text="DebuggableAttribute.DebuggingModes 
                Enumeration" 
                Value="DebuggableAttribute.DebuggingModes 
                Enumeration"></asp:TreeNode>
                <asp:TreeNode 
                Text="EventLogPermissionEntryCollection 
                Class" 
                Value="EventLogPermissionEntryCollection 
                Class"></asp:TreeNode>
                </asp:TreeNode>
                </asp:TreeNode>
                </Nodes>
                </asp:TreeView>    
              </div>            
            </td>
            <td id="tdMid1" 
            style="height:200px;width:6px;
            background-color:lightsteelblue;"></td>
            <td id="tdEdit1" 
            align="left" valign="top" 
            style="height:200px;"> 
                    <textarea runat="server" 
                    id="txtEdit1" 
                    style="margin:0px;height:100%;width:100%;
                    padding:0px 0px 0px 5px;
                    border:none;"></textarea>
            </td>
        
        </tr>
    </table>
    <span style="font-family:Verdana:font-size:10pt;">
    Current Width: 
    </span>
    <input type="text" id="txtWidth1" 
    value="250px" 
    style="width:70px;font-family:Verdana:font-size:10pt;"/>
    </div>

More Usage Examples

You can download many other examples of how to use the SplitterBar by clicking the 'Download demo' link at the top of this article.

Points of Interest

The most challenging part about building this control was the JavaScript and DHTML, because I wanted it to work in both IE and Firefox. At first, it seemed like the task would be pretty straightforward, but as I started testing and building the demo project examples, I kept finding little bugs that needed to be addressed. It can become pretty time consuming to hunt down all of these little issues, and I did my best to try to deal with most of them. If you find bugs, feel free to fix them and reply to this article with your suggestions for improvements.

When you try to implement the SplitterBar, you may find that your page is not behaving as you expected. I would like to warn you in advance about this - most of the issues that I found after I completed the control could be addressed by adjusting the CSS styles on the table or controls that I was applying the SplitterBar onto. Many times, our initial reaction is to think that a problem with the column resizing means that there is something wrong with the SplitterBar, and that is certainly possible, but I found that not to be the case most of the time.

The DOCTYPE (markup format specification) that you choose will have a profound impact on how your content is rendered by the browser. If you are having trouble getting styles to work as you expect, you may want to try using a different DOCTYPE. If you are getting strange results or it seems like the browser is ignoring your style settings, it it is interesting and worth the time to try rendering the page with no DOCTYPE at all. Completely remove it from the document, as some browsers, like IE 7, will still pick up the DOCTYPE even if it is commented out.

The demo project Web Forms are using the following DOCTYPE:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

History

I have updated the VwdCms.SplitterBar based on some of the suggestions that you have made. Here is a list of the what has been added or changed:

  • Built in persistence of column width after a postback.
  • Better support for dealing with IFrames.
  • Clean-up of the JavaScript code in VwdCmsSplitterBar.js.
  • More code examples in the demo project including postbacks.
  • Ability to specify how the SplitterBar will handle IFrames.
  • An entire demo page dedicated to IFrames.
  • Double-clicking to minimize and maximize the column width.
  • Changed the OnResizeFunction event to OnResize.
  • Added the OnResizeStart event.
  • Added the OnResizeComplete event.
  • Added the OnDoubleClick event.
  • Added code examples for using the OnResizeStart, OnResize, and OnResizeComplete event handlers in the demo project.
  • Updated to support Master Pages and Container Controls (Naming Containers):
    • C# and JavaScript code work together to determine the ClientID of an element.
    • SplitterBar checks to see if it is in a NamingContainer so it can find the controls during the postback.
    • Added a new example (#7) to the demo project to demonstrate the use of the SplitterBar with Master Pages.
  • Added the link to the SplitterBar online demo.
  • Added a GridView Column Resizing example to the demo project.
  • Added a Horizontal Row Resizing functionality to the SplitterBar, and a new example to the demo project.
  • Added code to account for the window/document scrollLeft and scrollTop so the SplitterBar will continue to work properly when the page has been scrolled down or to the right.
  • Added Example 10 to the demo project - this is an example of using the Vertical and Horizontal Splitter Bars together to create a table with a resizable left column, and in the right column there are two rows which are resizable.
  • Added persistence during postbacks for the MinWidth, MaxWidth, MinHeight, and MaxHeight properties.
  • Updated Example 10 in the demo project to demonstrate setting the MinWidth, MaxWidth, MinHeight, and MaxHeight properties.
  • Added better support for server side resizing. Now, setting the SplitterBar.LeftColumnWidth or SplitterBar.TopRowHeight will automatically resize the target controls during a postback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jeff Bazinet
Web Developer
United States United States
Member
VWD-CMS is an ASP.NET content management system designed and developed by Jeff Bazinet.

While VWD-CMS has been developed entirely in Visual Web Developer, it will power any ASP.NET 2.0 website.

VWD-CMS is a file based content management system that has been built with developers in mind -

one of the primary design requirements is that the CMS does not constrain or limit a developer.

Try the online demo and learn more about VWD-CMS at www.vwd-cms.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNamespace Errormemberviru5detected15 Apr '13 - 16:27 
I'm getting the following error when I try to compile my application:
 
Quote:
The type or namespace name 'VwdCms' could not be found in the global namespace (are you missing an assembly reference?)

 
I'm using VS2010 with ASP4.0
Questionhow to use RightResizetargets propertymemberShrish198612 Dec '12 - 1:20 
Sir thanx for this control..
But my problem is what if i Flip the whole view vertically. every thing which is right to left becomes left to right. Now the controls for resizing will be on right of the splitter.
I have used the right resize property , but this is not working. plz help..
my email id is n.shrish@hotmail.com..
plz answer this query..
QuestionThe splitter is showing some width away from the td.memberShrish19863 Dec '12 - 19:53 
On page load the splitter is showing at a certain width away from the where i have applied it, but when i resize the td , the alignment becomes correct, and splitter is shown at the seperation.. any help
GeneralProblem with the splitter positionmemberMember 810781115 Jul '12 - 22:04 
I know this topic has been posted years ago but I have read it just a few days ago.
 
I am creating a vb.net site with a master page and tried your code in one of the pages.
 
It is just a simple table to check whether it will work correctly or not.
 
<VwdCms:SplitterBar ID="hsbHaulage" Orientation="Horizontal"
        TopResizeTargets="tdTop" BottomResizeTargets="tdBottom"
        DynamicResizing="true" runat="server"
        MinHeight="30" MaxHeight="370" TotalHeight="406"
        style="background-image:url(App_Theme/images/hsplitter.gif);background-position:center center;background-repeat:no-repeat;">
    </VwdCms:SplitterBar>
 
<table width="96%" border="1">
        <tr>
            <td id="tdTop">
                This example demonstrates a Horizontal VwdCms.SplitterBar implemented with
                    a table that has a fixed height and contains two rows/cells.
                    This example demonstrates a Horizontal VwdCms.SplitterBar implemented with
                    a table that has a fixed height and contains two rows/cells.
                    This example demonstrates a Horizontal VwdCms.SplitterBar implemented with
                    a table that has a fixed height and contains two rows/cells.
                    This example demonstrates a Horizontal VwdCms.SplitterBar implemented with
                    a table that has a fixed height and contains two rows/cells.
            </td>
        </tr>
        <tr>
            <td id="tdBottom">
                Test 2
            </td>
        </tr>
    </table>
 

I am using IE7. The splitter is appearing around 2 inches below the actual separator of the top and bottom rows. Can you help me?
 
I would like to show you the screen shot of what's happening but I don't know how to post an image.
 
I am not sure where to change the postition of the splitter? Isn't it automatic between the two rows it is dividing?
Questionsplitterbar not workingmembervikas@199016 May '12 - 21:56 
hi friends,
i have added this splliterbar with a leftsided div.
this div is inside updatepannel and having a treeview which is popoulating from table.all including splitter bar is working fine.but when i add a node to treeview and then click on splliterbar a runtime error occures errror is---- htmlfile: Unspecified error. and a line is coloured in vwdcmssplitter.js file line is...
var op = sbar.primaryResizeTarget.offsetParent;
 
my all control control is inside updatepannel excluding splliterbar.when i put splliterbar inside updatepannel it gives error.
 
please help
GeneralMy vote of 5membermanoj kumar choubey16 Feb '12 - 0:21 
Nice
QuestiononResizeFunction javascript function never firedmemberjncharli4 Jan '12 - 12:18 
Hi Jeff,
 
First of all, many thanks for such a great work.
 
I nevertheless have an issue.
I made a very simple test : the splitter bar works great, but the javascript function I set to the onResizeFunction parameter is never fired.
 
I tested with FF and IE, but none of the browser fires the javascript.
 
I had a look in the previous posts, but found nothing about a similar issue.
Am I the only one to have this issue (that would suggest I make a rough error) or is this an issue you already know ?
 
Best regards,
 
Some code ?
 
TestSplitterBar.aspx
 
 
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="TestSplitterBar.aspx.cs" Inherits="_Default" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
 
<br />
<br />
 
<script language="javascript" type="text/javascript"><!--
 
    function splitterOnResize(width) {
        // do any other work that needs to happen when the 
        // splitter resizes. this is a good place to handle 
        // any complex resizing rules, etc.
 
        alert("splitterOnResize");
 
        // make sure the width is in number format
        if (typeof width == "string") {
            width = new Number(width.replace("px", ""));
        }
 
        // now do something...
    }
    
// -->
</script>
 
<VwdCms:SplitterBar runat="server" ID="vsbSplitter1" 
    LeftResizeTargets="tdGauche;divGauche" 
    MinWidth="100" 
    MaxWidth="700"
    BackgroundColor="lightsteelblue" 
    BackgroundColorLimit="firebrick"
    BackgroundColorHilite="steelblue"
    BackgroundColorResizing="purple"
    SaveWidthToElement="txtWidth1"    
    OnResizeFunction="splitterOnResize"
    style="background-image:url(images/cmnSplitter.gif);
        background-position:center center;
        background-repeat:no-repeat;"/>
 
 <table width="100%">
    <tr>
        <td id="tdGauche">
            <div id="divGauche" style="width: 100%">
                <textarea id="TextArea1" cols="20" rows="2" style="width: 100%"></textarea>
            </div>
        </td>
        <td id="tdDroite">
            <div id="divDroite" style="width: 100%">
                <textarea id="TextArea2" cols="20" rows="2" style="width: 100%"></textarea>
            </div>
        </td>
    </tr>
</table>
 
<input type="text" id="txtWidth1" 
    value="250px" 
    style="width:70px;font-family:Verdana:font-size:10pt;"/>
 
<br />
</asp:Content>
 

 
 
TestSplitterBar.aspx.cs
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using log4net;
using log4net.Config;
using System.Reflection;
using System.Data.Linq.Mapping;
using System.Collections.ObjectModel;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    // Log attributes
    // ==============
    private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
    // Constants
    // =========
    private const string SPLITTERBARJAVASCRIPTNAME = "VwdCmsSplitterBar.js";
    private const string JAVASCRIPTNAME = "TestSplitterBar.js";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        string sFunctionName = "Page_Load";
 
        _log.Debug(sFunctionName);
 

        // Common instructions whatever initialization or postback
        // -------------------------------------------------------
        // register the javascript scripts
        // -------------------------------
        JavascriptRegisterer.RegisterClientScriptBlock(Page, this.GetType(), SPLITTERBARJAVASCRIPTNAME, SPLITTERBARJAVASCRIPTNAME);
        JavascriptRegisterer.RegisterClientScriptBlock(Page, this.GetType(), JAVASCRIPTNAME, JAVASCRIPTNAME);                
    }
}
 
 
TestSplitterBar.js
 
 
window.onload = splitterOnResize("");
 

function splitterOnResize(width) {
 
    alert("splitterOnResize");
 
    document.getElementById("txtWidth1").value = "xx";
 
    // do any other work that needs to happen when the 
    // splitter resizes. this is a good place to handle 
    // any complex resizing rules, etc.

    // make sure the width is in number format
    if (typeof width == "string") {
        width = new Number(width.replace("px", ""));
    }
 
    // now do something...
}
 

GeneralProblem with "splitter bar" being offset out of tablememberHubman12 Sep '10 - 14:05 
Hi -
 
Cool program, but I am having a problem with a two column table. The splitter bar is in the right place, but it is about 20px below the top of the table AND it protrudes out the bottom of the table by the same distance (20px). I suspect this is some sort of style sheet issue, but I can't figure it out. I am using Visual Studio 2010 and a master document.
 
Thanx!
GeneralRe: Problem with "splitter bar" being offset out of tablememberHubman12 Sep '10 - 15:41 
OK - It's not a table or CSS that I define. I have a asp menu control above the table I have the splitter bar linked to. For some reason the menu control makes the slitter drop down and actually extends out of the bottom of the table I have it linked to. Example code below...
 
<form runat="server">
 
<table border="0" cellpadding="0" cellspacing="0" style="width:100%; ">
    <tr style="color:White; background-color:#4b6c9e;">
        <td style="text-align:left; vertical-align:middle; font-weight: bold; font-size: 40px; padding-left:20px; padding-top:10px; padding-bottom:10px;" >
        code Finder
        </td>
        <td style="text-align:right; color: White; padding-right: 15px;" >
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                <AnonymousTemplate>
                    [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" style="color:White;" runat="server">Log In</a> ]
                </AnonymousTemplate>
                <LoggedInTemplate>
                    Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                    [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
                </LoggedInTemplate>
            </asp:LoginView>        
        </td>
    </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" style="width:100%; ">
    <tr style="background-color: #465c71;">
        <td>
            <asp:Menu ID="menuMain" runat="server" Orientation="Horizontal" 
            BackColor="#465c71" Font-Bold="False" ForeColor="White" 
            BorderColor="#465c71" BorderStyle="None" BorderWidth="1px" 
            DynamicHorizontalOffset="8" DynamicVerticalOffset="8">
                <StaticMenuStyle HorizontalPadding="5px" />
                <DynamicHoverStyle BackColor="#0033CC" ForeColor="White" />
                <DynamicMenuStyle BackColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
                <StaticHoverStyle BackColor="#0033CC" ForeColor="White" />
                <Items>
                    <asp:MenuItem NavigateUrl="~/Default.aspx" Text=" Home " Value="Home"></asp:MenuItem>
                    <asp:MenuItem Text=" Edit " Value="Edit">
                        <asp:MenuItem Text="Item 1" Value="Item1" NavigateUrl=""></asp:MenuItem>
                        <asp:MenuItem Text="Item 2" Value="Item2" NavigateUrl=""></asp:MenuItem>
                    </asp:MenuItem>
                </Items>
            </asp:Menu>  
        </td>
    </tr>
</table>
  <br />
 
<table border="0" style="width:100%;border:solid 1px black;" cellpadding="0" cellspacing="0">
    <tr>
        <td id="tdleft" style="width:30%; height:300px;" >
            left pane
        </td>
        <td id="tdright" style="background-color:Silver; padding-left: 5px;" >
            right pane<br />
        </td>
    </tr>
</table>
 
<div id="txtWidth1" style="text-align:center; " ></div>
 
<vwdcms:splitterbar runat="server" ID="vsbSplitter2" 
    LeftResizeTargets="tdleft" 
    MinWidth="150" 
    MaxWidth="600"
    BackgroundColor="lightsteelblue" 
    BackgroundColorHilite="steelblue"
    BackgroundColorResizing="purple"
    BackgroundColorLimit="firebrick"
    DynamicResizing="true"
    SaveWidthToElement="txtWidth1"
    OnResize="splitterOnResize"
	style="background-image:url(vsplitter.gif); background-position:center center; background-repeat:no-repeat;"
  />    
 
</form>
 

 

GeneralRe: Problem with "splitter bar" being offset out of tablememberCloudCasts22 Jan '12 - 11:11 
I'm getting the same... I'll look into it...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 11 Mar 2008
Article Copyright 2007 by Jeff Bazinet
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid