<script type="text/javascript">
<!--
function getCheckBoxListItemsChecked(elementId)
{
var elementRef = document.getElementById(elementId);
var checkBoxArray = elementRef.getElementsByTagName('input');
var checkedValues = '';
for (var i=0; i<checkBoxArray.length; i++)
{
var checkBoxRef = checkBoxArray[i];
if ( checkBoxRef.checked == true )
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AFAIK, you cannot get the value property of a ListItem in a CheckBoxList.
// You can only get the Text property, which will be in an HTML label element.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if ( labelArray.length > 0 )
{
if ( checkedValues.length > 0 )
checkedValues += ', ';
quincarroll
Member
244 Points
94 Posts
How do you get the value from a CheckBoxList using JavaScript
May 08, 2009 12:56 PM|LINK
Hi
I have the following:
<asp:Button ID="Button2" runat="server" Text="Load Companies" OnClick="LoadCompanyData" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:CheckBoxList ID="cblCompany" CssClass="lbl8" runat="server" AutoPostBack="false" CausesValidation="false" DataValueField="coName">
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
I fill cblCompany with this code behind:
protected void LoadCompanyData(object sender, EventArgs e)
{
String lvSelectedIndustries = txtIndustryWhereClause.Text;
lvCommand = "Select Distinct coName, coID From vwDirector Where" +
" SectorCD in (" + lvSelectedIndustries + ") Order By coName";
SqlConnection cn = new SqlConnection(PV.GetConnectionString());
lvSQL = new SqlCommand(lvCommand, cn);
dsTemp = Functions.SQL.pfReturnDataSet(lvSQL);
Int32 lvCount = Functions.SQL.CheckDataSet(dsTemp);
cblCompany.Items.Clear();
if (lvCount > 0)
{
foreach (DataRow row in dsTemp.Tables[0].Rows)
{
ListItem lvItem = new ListItem(Functions.SQL.FixNull(row, "coName"), Functions.SQL.FixNull(row, "coID"));
if (lvItem.Text != "")
{
lvItem.Attributes.Add("onclick", "SetSelectedCompany(this);");
cblCompany.Items.Add(lvItem);
}
}
}
}
Not very complex.
I am trying to find out what the "Value" of the clicked check box in cblCompany is. If I use:
function SetSelectedCompany(obj)
{
var lvValue=obj.value;
}
lvValue ends up being"on". This is not what I am looking for.
I am also not looking to know what the "Checked" property is as I already know how to get it.
I need to know what the Value of the clicked checkbox is, as in the Value that is inserted by:
ListItem lvItem = new ListItem(Functions.SQL.FixNull(row, "coName"), Functions.SQL.FixNull(row, "coID"));
The part that holds the "coID".
Any Ideas anyone?
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: How do you get the value from a CheckBoxList using JavaScript
May 08, 2009 01:34 PM|LINK
The Value property is a server-side only property. You can only get the Text property client-side
<form id="form1" runat="server">
<asp:CheckBoxList id="CheckBoxList1" runat="server">
<asp:listitem Value="1">Item 1</asp:listitem>
<asp:listitem Value="2">Item 2</asp:listitem>
<asp:listitem Value="3">Item 3</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="readCheckBoxList();" value="Read CheckBoxList" />
</form>
<script type="text/javascript">
<!--
function getCheckBoxListItemsChecked(elementId)
{
var elementRef = document.getElementById(elementId);
var checkBoxArray = elementRef.getElementsByTagName('input');
var checkedValues = '';
for (var i=0; i<checkBoxArray.length; i++)
{
var checkBoxRef = checkBoxArray[i];
if ( checkBoxRef.checked == true )
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AFAIK, you cannot get the value property of a ListItem in a CheckBoxList.
// You can only get the Text property, which will be in an HTML label element.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if ( labelArray.length > 0 )
{
if ( checkedValues.length > 0 )
checkedValues += ', ';
checkedValues += labelArray[0].innerHTML;
}
}
}
return checkedValues;
}
function readCheckBoxList()
{
var checkedItems = getCheckBoxListItemsChecked('<%= CheckBoxList1.ClientID %>');
alert('Items checked: ' + checkedItems);
}
// -->
</script>
NC...
praveen.batt...
Member
722 Points
151 Posts
Re: How do you get the value from a CheckBoxList using JavaScript
Sep 30, 2009 02:30 PM|LINK
Try this solution.
http://praveenbattula.blogspot.com/2009/09/aspnet-checkboxlist-get-values-in.html
Let me know, if you have any questions still..
jquery checkbox list javascipt values
Site: Rare Solutions