Saturday 5 July 2014

DropDown List With Multiple Selection Option in SharePoint 2010

Introduction 
This article shows how to create a DropDown list with multiple selection options. This custom control DropDown allows multiple selections. In an ASP.Net DropDown we select only one option at a time.

DropDown with multiple selections Screen Shot 

Dropdown with multiple selections

Aspx code
<style type="text/css">
    .DivClose
    {
        displaynone;
        positionabsolute;
        width150px;
        height220px;
        border-stylesolid;
        border-colorGray;
        border-width1px;
        background-color#F0FFFF;
    }
   
    .LabelClose
    {
        vertical-aligntext-top;
        positionabsolute;
        bottom0px;
        font-familyVerdana;
    }
   
    .DivCheckBoxList
    {
        displaynone;
        background-colorWhite;
        width150px;
        positionabsolute;
        height200px;
        overflow-yauto;
        overflow-xhidden;
        border-stylesolid;
        border-colorGray;
        border-width1px;
        width150px;
    }
   
    .CheckBoxList
    {
        positionrelative;
        width150px;
        height10px;
        overflowscroll;
        font-sizesmall;
    }
    .Maintitle
    {
        text-alignleft;
        margin0em;
        color#00008B;
        font-sizelarge;
        padding-bottom16px;
        border-bottom2px solid #A9A9A9;
        margin-bottom10px;
        white-spacenowrap;
    }
    .headers
    {
        text-alignleft;
        padding-right10px;
    }
   
    .numbers
    {
        text-alignright;
        padding-right12px;
    }
    .textData
    {
        text-alignleft;
        padding-right10px;
    }
    .numbersHeader
    {
        width80px;
    }
    .numbersfooter
    {
        background-color#d2d0d0;
        color#000000;
    }
   
    .SGridView
    {
        width100%;
    }
    .style1
    {
        height30px;
    }
</style>
<asp:Literal ID="LtrMessage" runat="server"></asp:Literal>

<table style="padding-top: 9px">
    <tr>      
        <td style="padding-bottom: 13px; font-style: normal;">
            <strong>City :</strong><span style="color: Red; padding-left: 2px;">*</span>
        </td>
        <td>
            <div id="divCustomCheckBoxList" runat="server" onmouseover="clearTimeout(timoutID);"
                onmouseout="timoutID = setTimeout('HideMList()', 750);">
                <table>
                    <tr>
                        <td align="right" class="style1">
                            <input id="txtSelectedMLValues" type="text" readonly="readonly" onclick="ShowMList()"
                                style="width: 150px;" runat="server" />
                        </td>
                        <td align="left" class="style1">
                            <img id="imgShowHide" runat="server" align="left" onclick="ShowMList()"src="~/_layouts/1033/images/down-arrow.bmp" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="DropDownLook">
                            <div>
                                <div runat="server" id="divCheckBoxListClose" class="DivClose">
                                    <label runat="server" onclick="HideMList();" class="LabelClose" id="lblClose">
                                        <b>x</b>
                                    </label>
                                </div>
                                <div runat="server" title="divCheckBoxList" id="divCheckBoxList" class="DivCheckBoxList">
                                    <asp:UpdatePanel ID="UpdatePanel11" runat="server">
                                        <ContentTemplate>
                                            <asp:CheckBox ID="CheckBoxList1" runat="server" Text="All" AutoPostBack="true"OnCheckedChanged="CheckBoxList1_CheckedChanged">
                                            </asp:CheckBox>
                                            <asp:CheckBoxList ID="lstMultipleValues" runat="server" Width="250px"CssClass="CheckBoxList">
                                            </asp:CheckBoxList>
                                        </ContentTemplate>
                                    </asp:UpdatePanel>
                                </div>
                            </div>
            </div>
        </td>
        <td>
        </td>
    </tr>
</table>
<br />
<br />
<div>   
</div>
<div>
    <script type="text/javascript"> 
        var timoutID; 
        //This function shows the checkboxlist
        function ShowMList() {

            var divRef = document.getElementById("<%=divCheckBoxList.ClientID%>");
            divRef.style.display = "block";
            var divRefC = document.getElementById("<%=divCheckBoxListClose.ClientID%>");
            divRefC.style.display = "block";

        }

        //This function hides the checkboxlist
        function HideMList() {
            document.getElementById("<%=divCheckBoxList.ClientID%>").style.display = "none";
            document.getElementById("<%=divCheckBoxListClose.ClientID%>").style.display = "none";
        }
     
        var cblstTable;
        var checkBoxPrefix;
        var noOfOptions;
        var selectedText
        function FindSelectedItems(sender, textBoxID) {
            cblstTable = document.getElementById(sender.id);
            var flagUnchecked = 0;
            var flaChecked = 0;
            checkBoxPrefix = sender.id + "_";
            noOfOptions = cblstTable.rows.length;
            selectedText = "";
            for (i = 0; i < noOfOptions; ++i) {
                if (document.getElementById(checkBoxPrefix + i).checked) {
                    flag = 1;

                    if (selectedText == "")
                        selectedText = document.getElementById
                                   (checkBoxPrefix + i).parentNode.innerText;
                    else
                        selectedText = selectedText + "," +
                 document.getElementById(checkBoxPrefix + i).parentNode.innerText;
                }
            }
            for (i = 0; i < noOfOptions; ++i) {
                if (document.getElementById(checkBoxPrefix + i).checked == false) {
                    document.getElementById("<%=CheckBoxList1.ClientID%>").checked = false;
                }
                if (document.getElementById(checkBoxPrefix + i).checked == true) {

                    flagUnchecked = 1;
                    flaChecked = flaChecked + 1;
                }
            }
            document.getElementById(textBoxID.id).value = selectedText;
            if (flagUnchecked == 0) {
                document.getElementById(textBoxID.id).value = "Select City";
            }
            if (flaChecked == noOfOptions) {
                document.getElementById("<%=CheckBoxList1.ClientID%>").checked = true;
                document.getElementById(textBoxID.id).value = "All City";
            }
        }
        function FindAllRepMgr(sender, textBoxID) {

            if (document.getElementById("<%=CheckBoxList1.ClientID%>").checked == true) {
                for (i = 0; i < noOfOptions; ++i) {
                    var raw = document.getElementById(checkBoxPrefix + i).rawValue = null;
                    var raw1 = document.getElementById(checkBoxPrefix + i).checked = true;
                    document.getElementById(checkBoxPrefix + i).checked == true;
                }
                document.getElementById(textBoxID.id).value = "All City";
            }
            if (document.getElementById("<%=CheckBoxList1.ClientID%>").checked == false) {
                for (i = 0; i < noOfOptions; ++i) {
                    var raw = document.getElementById(checkBoxPrefix + i).rawValue = null;
                    var raw1 = document.getElementById(checkBoxPrefix + i).checked = false;
                    document.getElementById(checkBoxPrefix + i).checked == false;
                }
                document.getElementById(textBoxID.id).value = "Select City";
            }
        }
    </script>
</div>
C#: Cs.code

CS code
 
 
 
 

No comments:

Post a Comment

SharePoint 2013 - Uploading Multiple Attachments To The New Item On List Using JSOM And REST API

  Introduction In this article, we will explore how we can attach multiple attachments to the new item on list using JSOM and REST API. Ther...