Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Javascript

How to Get the CheckBoxlist Value using JavaScript?

Rate me:
Please Sign up or sign in to vote.
4.44/5 (9 votes)
18 Sep 2007CPOL 92.9K   18   10
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:

C#
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.

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

Step 4

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

C#
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:

C#
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)


Written By
Software Developer (Senior) Tata Consultancy Services Ltd
India India
Trilochan Nayak is a B.TECH graduate from Institute of Engineering and Management,Kolkata.

He is proficient in:

- ASP.NET- more than 6+ years of experience.
- Sharepoint- more than 6+ years of experience.
- C#- more than 6+ years of experience.
- VB.NET- more than 6+ years of experience.
- JavaScript - more than 6+ years of experience.
- SQL SERVER- more than 6+ years of experience.
- Oracle- more than 6+ years of experience.
- Visual Basic- more than 2+ years of experience.
- ASP- more than 2+ years of experience.
- PHP- more than 2+ years of experience.

His blog site is:
trilochan-nayak.spaces.live.com

Comments and Discussions

 
QuestionNice one Pin
Member 1338498229-Aug-17 21:07
Member 1338498229-Aug-17 21:07 
GeneralThanks Pin
Ravish Ranjane7-Oct-09 5:56
Ravish Ranjane7-Oct-09 5:56 
GeneralGetting only the value of the first item Pin
palluCodeProject7-Mar-09 15:29
palluCodeProject7-Mar-09 15:29 
GeneralGreat Article Pin
rameshsethu2-Dec-08 10:55
rameshsethu2-Dec-08 10:55 
AnswerStartIndex error & persistance issues Pin
Member 97937818-Jun-08 2:23
Member 97937818-Jun-08 2:23 
GeneralRe: StartIndex error & persistance issues Pin
Trilochan Nayak.13-Aug-08 21:24
Trilochan Nayak.13-Aug-08 21:24 
GeneralGood Article Pin
Anil R28-Mar-08 5:00
Anil R28-Mar-08 5:00 
But, I'm getting the following error, When I select checkboxes in the chekboxlist and click on Submit button As follows:

startIndex cannot be larger than length of string.
Parameter name: startIndex

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex

Stack Trace:


[ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex]
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +2819819
System.Web.UI.WebControls.CheckBoxList.LoadPostData(String postDataKey, NameValueCollection postCollection) +54
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +11
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +408
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3776

Anil Kumar Vattam

GeneralUseful Pin
Ken Hales19-Oct-07 8:51
Ken Hales19-Oct-07 8:51 
GeneralHey dude u helped me..one of a article of it's kind Pin
Ritesh K Singh19-Sep-07 4:06
Ritesh K Singh19-Sep-07 4:06 
GeneralGreat Artilcle Pin
Matrix.18-Sep-07 21:04
Matrix.18-Sep-07 21:04 

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

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