Click here to Skip to main content
Licence CPOL
First Posted 18 Sep 2007
Views 52,272
Bookmarked 18 times

How to Get the CheckBoxlist Value using JavaScript?

By | 18 Sep 2007 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Trilochan Nayak.

Software Developer (Senior)
Tata Consultancy Services Ltd
India India

Member

Trilochan Nayak is a B.TECH graduate from Institute of Engineering and Management,Kolkata.
 
He is proficient in:
 
- ASP.NET- more than 4+ years of experience.
- C#- more than 4+ years of experience.
- VB.NET- more than 4+ years of experience.
- JavaScript - more than 4+ years of experience.
- SQL SERVER- more than 4+ years of experience.
 
Also familiar with:
 
- ASP;
 
- PHP;
 
His blog site is:
trilochan-nayak.spaces.live.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks PinmemberRavishRanjane5:56 7 Oct '09  
GeneralGetting only the value of the first item PinmemberpalluCodeProject15:29 7 Mar '09  
GeneralGreat Article Pinmemberrameshsethu10:55 2 Dec '08  
One little thing to add to the class also would be
 
If Items(repeatIndex).Enabled = False Then
writer.WriteAttribute("disabled", "disabled")
End If
 
the reason being, let's say u disable the Checkbox control on the server side using .enabled = false, IE will properly disable the check-boxes, but Mozilla does not
 
If you look at the source code the, the check-boxes have a table wrapper and in both browsers, the table is disabled by disabled = disabled, but somehow Mozilla does not disable it...
 
In order to overcome that, you have to explicitly disable each individual checkbox item with the line above and on the server side, let's say you got the checkbox control
 
do the following on the server side...
 
For i as integer = 0 to chk.items.count -1
chk.items(i).enabled = false
Next
 
then what happens is the code in the class file correctly adds the disabled property to the individual checkboxes and things work perfectly fine.
 
Just wanted to add this if someone ran into this issue....
AnswerStartIndex error & persistance issues PinmemberMember 9793782:23 18 Jun '08  
GeneralRe: StartIndex error & persistance issues PinmemberTrilochan Nayak.21:24 13 Aug '08  
GeneralGood Article PinmemberAnil R5:00 28 Mar '08  
GeneralUseful PinmemberKen Hales8:51 19 Oct '07  
GeneralHey dude u helped me..one of a article of it's kind PinmemberRitesh K Singh4:06 19 Sep '07  
GeneralGreat Artilcle PinmemberMatrix.21:04 18 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 19 Sep 2007
Article Copyright 2007 by Trilochan Nayak.
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid