Al Pascual



Geo RSS
Geo Twitter Timeline

Blogs I read

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Code Snip "AJAX Select All Check Box List Extender"

Provided by Scott Osborne 
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using AjaxControlToolkit;


[assembly: System.Web.UI.WebResource("AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.js", "text/javascript")]

namespace AjaxControlToolkit.SelectAllCheckBoxList
{
    [Designer(typeof(SelectAllCheckBoxListDesigner))]
    [ClientScriptResource("AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior", "AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.js")]
    [TargetControlType(typeof(Control))]
    public class SelectAllCheckBoxListExtender : ExtenderControlBase
    {
        // TODO: Add your property accessors here.
        //
        [ExtenderControlProperty]
        [DefaultValue("")]
        public string AllText
        {
            get
            {
                return GetPropertyStringValue("AllText");
            }
            set
            {
                SetPropertyStringValue("AllText", value);
            }
        }

        [ExtenderControlProperty]
        [DefaultValue("1")]
        public string ColSpan
        {
            get
            {
                CheckBoxList list = (CheckBoxList)this.TargetControl;
                return list.RepeatColumns.ToString();
                //return GetPropertyStringValue("ColSpan");
            }
            set
            {
                SetPropertyStringValue("ColSpan", value);
            }
        }
    }
}

 SelectAllCheckBoxDesigner.cs 

// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


using System.Web.UI.WebControls;
using System.Web.UI;

namespace AjaxControlToolkit.SelectAllCheckBoxList
{
    class SelectAllCheckBoxListDesigner : AjaxControlToolkit.Design.ExtenderControlBaseDesigner
    {


    }
}

  SelectAllCheckBoxListBehavior.js

// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('AjaxControlToolkit.SelectAllCheckBoxList');

AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior = function(element) {

    AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    //
    this._allText = null;
    this._colSpan = null;
    this.checkBoxList = null
    this.checkBoxes = null;
    this.allCheckBox = null;
    this.allCheckBoxLabel = null;
    this.parentNode = null;
    this.paraNode = null;
    this.checkBoxClickHandler = null;
    this.allCheckBoxClickHandler = null;

}

AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.prototype = {

    initialize : function() {
        AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.callBaseMethod(this, 'initialize');

        // TODO: add your initalization code here
                // get a reference to the list and all its elements
        this.checkBoxList = this.get_element();
        this.checkBoxes = this.checkBoxList.getElementsByTagName("input");
        
        // Create delegates
        this.checkBoxClickHandler = Function.createDelegate(this, this.onCheckBoxClick);
        this.allCheckBoxClickHandler = Function.createDelegate(this, this.onAllCheckBoxClick);
        
        // add click handlers for each item in the check box list
        for (var i = 0; i < this.checkBoxes.length; i++)
        {
            $addHandler(this.checkBoxes[i], "click", this.checkBoxClickHandler);
        }
        
        // get a reference to the containing element
        this.parentNode = this.checkBoxList.parentNode;
        
        // create the "AllCheckBox" element and label
        this.paraNode = document.createElement("p");
        this.allCheckBox = document.createElement("input");
        this.allCheckBox.type = "checkbox";
        this.allCheckBox.id = "AllCheckBox";
        this.allCheckBoxLabel = document.createElement("span");
        this.allCheckBoxLabel.innerHTML = this._allText;
        this.paraNode.appendChild(this.allCheckBox);
        this.paraNode.appendChild(this.allCheckBoxLabel);
        
        // all the "AllCheckBox" element to the top of the control
        var headerRow = document.createElement("tr");
        var headerCell = document.createElement("td");
        headerCell.colSpan = this._colSpan;
        headerCell.style.textAlign = "left";
        headerRow.appendChild(headerCell);
        headerCell.appendChild(this.paraNode);
        this.checkBoxList.tBodies[0].insertBefore(headerRow, this.checkBoxList.tBodies[0].firstChild);
        
        // add click handler for the "AllCheckBox" element
        $addHandler(this.allCheckBox, "click", this.allCheckBoxClickHandler);
    },

    dispose : function() {
        // TODO: add your cleanup code here

        AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.callBaseMethod(this, 'dispose');
    },

    // Property Assessors
    get_AllText : function() {
        return this._allText;
    },

    set_AllText : function(value) {
        this._allText = value;
    },
    
    get_ColSpan : function() {
        return this._colSpan;
    },

    set_ColSpan: function(value) {
        this._colSpan = value;
    },
    
    // Methods
    areAllElementsChecked : function() {
        for (var i = 1; i < this.checkBoxes.length; i++)
        {
            if (!this.checkBoxes[i].checked)
                return false;
        }

        return true;
    },
       
    selectAll : function() {
        for (var i = 0; i < this.checkBoxes.length; i++)
        {
            var checkBox = this.checkBoxes[i];
            checkBox.checked = true;
        }
    },
    
    deselectAll : function() {
        for (var i = 0; i < this.checkBoxes.length; i++)
        {
            var checkBox = this.checkBoxes[i];
            checkBox.checked = false;
        }
    },
    
    // Events
    onCheckBoxClick : function(eventElement) {
        var checkBox = eventElement.target; 
        if (this.areAllElementsChecked())
             this.allCheckBox.checked = true;
        else
             this.allCheckBox.checked = false;
    },
    
    onAllCheckBoxClick : function(eventElement) {
        var checkBox = eventElement.target;
        if (checkBox.checked)
            this.selectAll();
        else
            this.deselectAll();
    }
}

AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior.registerClass('AjaxControlToolkit.SelectAllCheckBoxList.SelectAllCheckBoxListBehavior', AjaxControlToolkit.BehaviorBase);
.