Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Get the CheckBoxlist Value using JavaScript?

0.00/5 (No votes)
18 Sep 2007 2  
I am using checkboxlist in ASP.NET 2.0 C#.I bind a list of records in checklistbox from DB. I could not get the value from JavaScript.

Introduction

We have to update the checkboxlist into a new control say name:"CheckBoxListExCtrl" which can inherit checkboxlist property.

Background

CheckBoxList extension.

Using the Code

Create a new control by the following steps.

Step 1

Create a class library project:

using System;
using System.Collections.Generic; 
using System.Text; 
using System.Globalization; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
namespace CheckBoxListExCtrl 
{ 
public class CheckBoxListExCtrl :CheckBoxList, IRepeatInfoUser 
{  
 void IRepeatInfoUser.RenderItem(ListItemType itemType, 
	int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer) 
 {
     writer.WriteBeginTag("input"); 
     writer.WriteAttribute("type", "checkbox"); 
     writer.WriteAttribute("name", UniqueID); 
     writer.WriteAttribute("id", ClientID + "_" + 
	repeatIndex.ToString(NumberFormatInfo.InvariantInfo)); 
     writer.WriteAttribute("value", Items[repeatIndex].Value); 
     System.Web.UI.AttributeCollection attrs = Items[repeatIndex].Attributes; 
     foreach (string key in attrs.Keys) 
     {
        writer.WriteAttribute(key, attrs[key]);
     }
     writer.Write(">"); 
     writer.Write(Items[repeatIndex].Text);
  }
} 
}  

Step 2

Build it into a *.dll file,and add it to toolbox of VS.NET. Then you can use it from your project.

Step 3

When we add this control into *.aspx page, the& following code is generated.

<checkboxlistexctrl id="CheckBoxListExCtrl1" runat="server"></checkboxlistexctrl>

Step 4

From the code behind, we have to write code to bind the CheckBoxListExCtrl control.

protected void PopulateCheckBoxListExCtrl() 
{
   CheckBoxListExCtrl.DataSource = DtRecords;//DataTable DtRecords from DB
   CheckBoxListExCtrl.DataTextField ="Column1"; 
   CheckBoxListExCtrl.DataValueField ="Column2"; 
   CheckBoxListExCtrl.DataBind();
}

Step 5

Then we can get the checkbox list value easily by:

document.getElementByID("CheckBoxListExCtrl").value

History

  • 19th September, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here