Skip to main content

DropDownList Having CheckBoxes

<table>
      
<tr
>
            
<td valign="top" style="width
165px">
                  
<asp:PlaceHolder ID="phDDLCHK" runat="server"></asp:PlaceHolder
>
            
</td
>
            
<td valign
="top">
                  
<asp:Button ID="btn" runat="server" Text="Get Checked" OnClick="btn_Click" 
/>
            
</td
>
            
<td valign
="top">
                  
<asp:Label ID="lblSelectedItem" runat="server"></asp:Label
>
            
</td
>
      
</tr
></table><asp:HiddenField ID="hidList" runat="server" />

Step 2: Add below lines of code on page load.

protected void Page_Load(object sender, EventArgs e)
{
      DropDownList ddl = new DropDownList
();
      ddl.ID = "ddlChkList"
;
      ListItem lstItem = new ListItem
();
      ddl.Items.Insert(0, lstItem);
      ddl.Width = new Unit
(155);
      ddl.Attributes.Add("onmousedown""showdivonClick()"
);
      CheckBoxList chkBxLst = new CheckBoxList
();
      chkBxLst.ID = "chkLstItem"
;
      chkBxLst.Attributes.Add("onmouseover""showdiv()"
);
      DataTable
 dtListItem = GetListItem();
      int
 rowNo = dtListItem.Rows.Count;
      string lstValue = string
.Empty;
      string lstID = string
.Empty;
      for (int
 i = 0; i < rowNo - 1; i++)
      {
            lstValue = dtListItem.Rows[i]["Value"
].ToString();
            lstID = dtListItem.Rows[i]["ID"
].ToString();
            lstItem = new ListItem("<a href=\"javascript:void(0)\" id=\"alst\" style=\"text-decoration:none;color:Black; \" onclick=\"getSelectedItem(' " + lstValue + "','" + i + "','" + lstID + "','anchor');\">" + lstValue + "</a>", dtListItem.Rows[i]["ID"
].ToString());
            lstItem.Attributes.Add("onclick""getSelectedItem('" + lstValue + "','" + i + "','" + lstID + "','listItem');"
);
            chkBxLst.Items.Add(lstItem);
      }
      System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div"
);
      div.ID = "divChkList"
;
      div.Controls.Add(chkBxLst);
      div.Style.Add("border""black 1px solid"
);
      div.Style.Add("width""160px"
);
      div.Style.Add("height""180px"
);
      div.Style.Add("overflow""AUTO"
);
      div.Style.Add("display""none"
);
      phDDLCHK.Controls.Add(ddl);
      phDDLCHK.Controls.Add(div);
}


Step 3:  
Place below javascript method in the aspx page  or you can place it in .js file and include it in the page.
showdiv method will be called on mouse over of div.
showdivonClick will be invoked on click of dropdownlist.
getSelectedItem will be called on click of checkbox and anchor.
check will be call on any click on the page basically it is used to hide the div on click of any where on the page apart from div.


<script language="javascript" type="text/javascript">
      
function
 showdiv() {
            document.getElementById("divChkList").style.display = "block"
;
      }

      function showdivonClick() {
            var objDLL = document.getElementById("divChkList"
);
            if (objDLL.style.display == "block"
)
                  objDLL.style.display = "none"
;
            else

                  objDLL.style.display = "block"
;
      }
      function getSelectedItem(lstValue, lstNo, lstID, ctrlType) {
            var
 noItemChecked = 0;
            var ddlReport = document.getElementById("ddlChkList"
);
            var selectedItems = ""
;
            var arr = document.getElementById("chkLstItem").getElementsByTagName('input'
);
            var arrlbl = document.getElementById("chkLstItem").getElementsByTagName('label'
);
            var objLstId = document.getElementById('hidList'
);
            for
 (i = 0; i < arr.length; i++) {
                  checkbox = arr[i];
                  if
 (i == lstNo) {
                        if (ctrlType == 'anchor'
) {
                              if
 (!checkbox.checked) {
                                    checkbox.checked = true
;
                              }
                              else
 {
                                    checkbox.checked = false
;
                              }
                        }
                  }
                  if
 (checkbox.checked) {
                        if (selectedItems == ""
) {
                              selectedItems = arrlbl[i].innerText;
                        }
                        else
 {
                              selectedItems = selectedItems + ","
 + arrlbl[i].innerText;
                        }
                        noItemChecked = noItemChecked + 1;
                  }
            }
            ddlReport.title = selectedItems;
            var
 Text = ddlReport.options[ddlReport.selectedIndex].text;
            if
 (noItemChecked == 1)
                  ddlReport.options[ddlReport.selectedIndex].text = lstValue;
            
else
                  
ddlReport.options[ddlReport.selectedIndex].text = noItemChecked + " Items"
;
            document.getElementById('hidList'
).value = ddlReport.options[ddlReport.selectedIndex].text;
      }
  
      
document.onclick = check;

      
function
 check(e) {
            var
 target = (e && e.target) || (event && event.srcElement);
            var obj = document.getElementById('divChkList'
);
            var obj1 = document.getElementById('ddlChkList'
);
            if (target.id != "alst" && !target.id.match("chkLstItem"
)) {
                  if (!(target == obj || target == obj1)) {

                        
obj.style.display = 'none'
                  
}
                  else if (target == obj || target == obj1) {

                        if (obj.style.display == 'block') {
                              obj.style.display = 'block'
;
                        }
                        else
 {
                              obj.style.display = 'none'
;
                              document.getElementById('ddlChkList'
).blur();
                        }
                  }
            }
      }
</script>
 

Step 4: btn_Click method will be used to get to get the selected checkbox status in the dropdownlist.

protected void btn_Click(object sender, EventArgs e)
{
      string strSelectedItem = string
.Empty;
      CheckBoxList chk = (CheckBoxList)phDDLCHK.FindControl("chkLstItem"
);
      DropDownList ddl = (DropDownList)Page.FindControl("ddlChkList"
);
      for (int
 i = 0; i < chk.Items.Count; i++)
      {
            if
 (chk.Items[i].Selected)
            {
                  if
 (strSelectedItem.Length == 0)
                  {
                        strSelectedItem = chk.Items[i].Selected.ToString();
                  }
                  
else
                  
{
                        strSelectedItem = strSelectedItem + ","
 + chk.Items[i].Selected.ToString();
                  }
            }
      }
      ddl.Items.Clear();
      ddl.Items.Add(new ListItem
(hidList.Value));
      lblSelectedItem.Text = strSelectedItem;
}

Step 5: Now add a method to get datatable which will be bind to checkboxlist.

public DataTable GetListItem()
{
      DataTable table = new DataTable
();
      table.Columns.Add("ID"typeof(int
));
      table.Columns.Add("Value"typeof(string
));
      table.Rows.Add(1, "ListItem1"
);
      table.Rows.Add(2, "ListItem2"
);
      table.Rows.Add(3, "ListItem3"
);
      table.Rows.Add(4, "My ListItem Wraps also"
);
      table.Rows.Add(5, "My New ListItem5"
);
      table.Rows.Add(6, "ListItem6"
);
      table.Rows.Add(7, "ListItem7"
);
      table.Rows.Add(8, "ListItem8"
);
      return
 table;
}


  

Comments

Popular posts from this blog

GRIDVIEW GROUPING

When displaying data, we sometimes would like to group data for better user experience or when displaying long list of hierarchal data, we would want to display them in a tree view kind of structure. There is more than way of doing this, but I am going to explain achieving this functionality using  AJAX Collapsible Panel Extender Control . Overview: I am going to use  Adventure Works  as datasource. Every product in  Production.Product  table belongs to a product sub category. We fetch handful of products and the sub categories they belong to from the database. Our objective is to list all the available sub categories and allow user to  expand/collapse  to look/hide the list of products belonging to each subcategory. Database Connection Added following entry under  connectionStrings  element in  web.config . < add   name = "Sql"   connectionString="Data Source=(local); Initial  Catalog = AdventureWorks ...

GRIDVIEW ZOOM IMAGE

When you have images in your  GridView , you would most likely show them as thumbnails so as to not distort the whole layout. However user would want to look at the full image by clicking on the image or just hovering his mouse over it. In today’s applications, this is a basic requirement and there are just so many third party controls or plugins which would support this functionality. I am going do this conventional way using  javascript  way in this article.  On top of it, I am also going to explain how to get images from database using  HttpHandlers . Example: I am using  Adventure Works  as datasource. We fetch handful of products and bind them to the grid. When page is initially loaded, we retrieve products from Production . Product  table and bind them to the grid. We display some product attributes such as Product ID, Product Number, Product Name, List Price and product’s thumbnail. When user hover his mouse on the page, we fetch the f...

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {     ...