In this article, I will show you how to use AJAX PopupControlExtender to build a multiple select DropDownList as this:
Firstly, we need an image captured from a standard drop down list as this:
I will use this image as the drop down list instead of the built-in ASP:DropDownList control, the reason is that when a drop down list is clicked, its own selections will be displayed and will overlap with the check box list. The following screen shot should explain this issue clearly:
Secondly, let’s add the drop down list image, a CheckBoxList, and an AJAX PopupControlExtender to a page.
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- <!– the style for divDDL will load
- the drop drop list image as background.
- –>
- <div id="divDDL" class="divDDL" runat="server">
- Please Select…
- </div>
- <asp:Panel ID="pnlCustomers" runat="server" CssClass="MultipleSelectionDDL">
- <asp:CheckBoxList ID="cblCustomerList" runat="server" onclick="readCheckBoxList()" >
- <asp:ListItem>Customer One</asp:ListItem>
- <asp:ListItem>Customer Two</asp:ListItem>
- <asp:ListItem>Customer Three</asp:ListItem>
- <asp:ListItem>Customer Four</asp:ListItem>
- <asp:ListItem>Customer Five</asp:ListItem>
- <asp:ListItem>Customer Six</asp:ListItem>
- <asp:ListItem>Customer Seven</asp:ListItem>
- </asp:CheckBoxList>
- </asp:Panel>
- <br />
- <cc1:PopupControlExtender ID="pceSelections" runat="server" TargetControlID="divDDL"
- PopupControlID="pnlCustomers" Position="Bottom" OffsetY="-16" >
- </cc1:PopupControlExtender>
- </ContentTemplate>
- </asp:UpdatePanel>
I use <div> tag with a style to load the drop down list image as background instead of adding the image directly, because I need “Please Select…” to show up on top of the image, so it looks like a real drop down list.
The AJAX PopupControlExtender is very straight forward and does not need much explanation. The CheckBoxList control is placed in a Panel control whose ID is assigned to the PopupControlExtender’s PopupControlID property.
You may notice there is a JavaScript function call attached to the CheckBoxList, this function is NOT a must for the above code to work. This JavaScript function is really an icing on the cake that makes the drop down list more “intelligent”: when user only selects one option from the CheckBoxList, the text “Please Select…” will change to the text of the selected option; if user selects more than one option, then the text “Please Select…” will change to “Multiple customers selected”. Here is the complete JavaScript functions:
- <script type="text/javascript">
- var hdnCount=0;
- function readCheckBoxList()
- {
- getSelectionCount();
- var cbl = document.getElementById('<%=cblCustomerList.ClientID%>');
- var browser = navigator.appName;
- var pos = 0;
- if (browser.indexOf("Microsoft") >=0)
- {
- pos = 0;
- }
- else
- {
- pos = 1;
- }
- var tbody = cbl.childNodes[pos];
- var length = tbody.childNodes.length – pos;
- if (hdnCount > 1)
- {
- var div = document.getElementById('<%=divDDL.ClientID%>');
- div.innerHTML = "Multiple customers selected";
- return;
- }
- else
- {
- for (i=0;i<length;i++)
- {
- var td = tbody.childNodes[i].childNodes[pos];
- var chk = td.childNodes[0];
- if (chk.checked)
- {
- var div = document.getElementById('<%=divDDL.ClientID%>');
- div.innerHTML = chk.nextSibling.innerHTML;
- }
- }
- }
- }
- function getSelectionCount()
- {
- var cbl = document.getElementById('<%=cblCustomerList.ClientID%>');
- var browser = navigator.appName;
- var pos = 0;
- if (browser.indexOf("Microsoft") >=0)
- {
- pos = 0;
- }
- else
- {
- pos = 1;
- }
- var tbody = cbl.childNodes[pos];
- var length = tbody.childNodes.length – pos;
- hdnCount = 0;
- for (i=0;i<length;i++)
- {
- var td = tbody.childNodes[i].childNodes[pos];
- var chk = td.childNodes[0];
- if (chk.checked)
- {
- hdnCount++;
- }
- }
- }
- </script>
Here is the styles I used, in case you want to give it a try:
- .MultipleSelectionDDL
- {
- border: solid 1px #000000;
- height: 100px;
- width: 331px;
- overflow-y: scroll;
- background-color: #f0f8ff;
- font-size: 11px;
- font-family: Calibri, Arial, Helvetica;
- line-height:normal;
- }
- .divDDL
- {
- padding-top:2px;
- padding-left:5px;
- width:335px;
- height:30px;
- background-image: url(images/DropDownList.png);
- background-position:-1px -2px;
- background-repeat:no-repeat;
- font-size:11px;
- font-family:Calibri, Arial, Helvetica;
- }
I will be very glad to know if it is helpful to you.