How to get the CheckBoxlist Value using Javascript?

Trilochan.nayak
Posted by in ASP.NET category on for Intermediate level | Views : 9505 red flag

To get the CheckBoxlist Value using Javascript, we have to update the checkboxlist into a new control say name:"CheckBoxListExCtrl " which can inherit checkboxlist property.


 Download source code for How to get the CheckBoxlist Value using Javascript?

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 following code is genereted .

<asp:checkboxlistexctrl id="CheckBoxListExCtrl111" runat="server" RepeatDirection="Horizontal"></asp: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

Page copy protected against web site content infringement by Copyscape

About the Author

Trilochan.nayak
Full Name: Trilochan Nayak
Member Level: Starter
Member Status: Member
Member Since: 11/7/2008 5:13:32 AM
Country: India

http://trilochannayak.wordpress.com/
am an associate with Tata Consultancy Services for the last 5 year with a total of 7+ years of work experience. I have worked primarily in the domain of Healthcare,Energy and Resources . My technological forte is C#, VB.NET, ASP, ASP.NET 2.0, ASP.NET 3.5, MOSS 2007, Sharepoint 2010, SQL SERVER 2000, SQL SERVER 2005,SQL SERVER 2008 and Oracle 11g. I have worked on business intelligence tools WebFocus.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)