Click here to Skip to main content
6,822,613 members and growing! (20,732 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

How to Get the CheckBoxlist Value using JavaScript?

By Trilochan Nayak.

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.
C# (C#2.0), Javascript, Windows, .NET (.NET2.0), Visual-Studio, Dev
Revision:2 (See All)
Posted:18 Sep 2007
Views:29,531
Bookmarked:16 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.74 Rating: 4.43 out of 5

1

2
1 vote, 14.3%
3

4
6 votes, 85.7%
5

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.


Member
Trilochan Nayak is a B.TECH graduate from Institute of Engineering and Management,Kolkata.

He is proficient in:

- ASP.NET- more than 2 years of experience.
- C#- more than 2 years of experience.
- JavaScript - more than 2 years of experience.
- SQL SERVER- more than 2 years of experience.

Also familiar with:

- ASP;

- PHP;

His blog site is:
trilochan-nayak.spaces.live.com
Occupation: Software Developer
Location: India India

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralThanks PinmemberRavishRanjane6:56 7 Oct '09  
GeneralGetting only the value of the first item PinmemberpalluCodeProject16:29 7 Mar '09  
GeneralGreat Article Pinmemberrameshsethu11:55 2 Dec '08  
AnswerStartIndex error & persistance issues PinmemberMember 9793783:23 18 Jun '08  
GeneralRe: StartIndex error & persistance issues PinmemberTrilochan Nayak.22:24 13 Aug '08  
GeneralGood Article PinmemberAnil R6:00 28 Mar '08  
GeneralUseful PinmemberKen Hales9:51 19 Oct '07  
GeneralHey dude u helped me..one of a article of it's kind PinmemberRitesh K Singh5:06 19 Sep '07  
GeneralGreat Artilcle PinmemberMatrix.22:04 18 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 Sep 2007
Editor: Deeksha Shenoy
Copyright 2007 by Trilochan Nayak.
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project