function GetText() {
var concatenatedString = "";
//Getting all the checkboxes.
var checkBoxList = $("#divContent input[type='checkbox']");
//Getting all the labels.
var labels = $("#divContent label");
for(index = 0; index < checkBoxList.length; index++)
{
//Concatenating the text for the checked checkboxes.
if(checkBoxList[index].checked)
{
concatenatedString += lables[index].innerText;
}
}
alert(concatenatedString);
return false;
}
YCS
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
dotnet_CH
Member
40 Points
260 Posts
Checkboxes on the form
Apr 10, 2012 08:04 AM|LINK
I have around 15 checkboxes on my asp.net form
All of them are server controls <asp:checkbox
I have following requirements for these checkboxes
1. On the click on button all the checkboxes get deselect
2. On the click on button all the text values of checkboxes get cancatinated into one string
Please provide code for these requirements
adeelehsan
All-Star
18287 Points
2740 Posts
Re: Checkboxes on the form
Apr 10, 2012 08:47 AM|LINK
Use the following:
FOR DESELECTING
protected void Button3_Click(object sender, EventArgs e) { foreach (Control ct in Page.Controls) { if (ct.HasControls()) { foreach (Control c in ct.Controls) { if (c is CheckBox) { ((CheckBox)c).Checked = false; } } } } }FOR CONCATENATION
protected void Button4_Click(object sender, EventArgs e) { String s = ""; foreach (Control ct in Page.Controls) { if (ct.HasControls()) { foreach (Control c in ct.Controls) { if (c is CheckBox) { s += ((CheckBox)c).Text + ","; } } } } Response.Write(s.Substring(0,s.Length-1)); }You can use StringBuilder for efficiency.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
chandrasheka...
Star
10258 Points
1760 Posts
Re: Checkboxes on the form
Apr 10, 2012 08:50 AM|LINK
Hi
try the following:
<div id="divContent"> <asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox1" /> <asp:CheckBox ID="CheckBox2" runat="server" Text="CheckBox2" /> <asp:CheckBox ID="CheckBox3" runat="server" Text="CheckBox3" /> <asp:CheckBox ID="CheckBox4" runat="server" Text="CheckBox4" /> <asp:Button ID="btnDeselect" runat="server" OnClientClick="Deseelct();" Text="Deselect" /> <asp:Button ID="btnGetText" runat="server" OnClientClick="GetText();" Text="Get Text" /> </div>JQuery script:
function Deseelct() { $("#divContent input[type='checkbox']:checked").attr("checked", false); return false; } function GetText() { var concatenatedString = ""; $("#divContent label").each(function() { concatenatedString += $(this).text(); }); alert(concatenatedString); return false; }Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
vish02chouha...
Member
303 Points
251 Posts
Re: Checkboxes on the form
Apr 10, 2012 08:53 AM|LINK
function toggleChecked() { $("#checkboxes input").each( function() { $(this).attr("checked",false); })//for checkbox uncheckedvar string;
function concat() { $("#checkboxes input").each( function() { if( $(this).attr("checked")==true){string+=$(this).attr("text");dotnet_CH
Member
40 Points
260 Posts
Re: Checkboxes on the form
Apr 11, 2012 09:06 AM|LINK
Thanks alot for the solutions - Chandrashekar
However in second solution, I am unable to get the values of checked check boxes, actually it is giving the string value of all the checkboxes
A1ien51
All-Star
29935 Points
5821 Posts
Re: Checkboxes on the form
Apr 11, 2012 12:13 PM|LINK
How many times are you going to ask the same question? 3?
Eric
dotnet_CH
Member
40 Points
260 Posts
Re: Checkboxes on the form
Apr 12, 2012 05:34 AM|LINK
I highly appreciate the respones I am getting from this forum.
However my problem is not getting resolve 100% due to certain reasons, that is why I am asking different questions on same problem.
If you see them carefully than you will find that my questions are changing in the message string.
chandrasheka...
Star
10258 Points
1760 Posts
Re: Checkboxes on the form
May 08, 2012 11:30 AM|LINK
Hi
Try the following method.
function GetText() { var concatenatedString = ""; //Getting all the checkboxes. var checkBoxList = $("#divContent input[type='checkbox']"); //Getting all the labels. var labels = $("#divContent label"); for(index = 0; index < checkBoxList.length; index++) { //Concatenating the text for the checked checkboxes. if(checkBoxList[index].checked) { concatenatedString += lables[index].innerText; } } alert(concatenatedString); return false; }Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
asteranup
All-Star
30184 Points
4906 Posts
Re: Checkboxes on the form
May 09, 2012 09:19 AM|LINK
Hi,
Try this code-
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#clearselection").click(function () { $(".container input[type=checkbox]").attr("checked", false); }); $("#getconcanated").click(function () { var selectedstring = $(".container input[type=checkbox]:checked").map(function (i,item) { return $("label[for="+item.id+"]").html(); }); debugger; alert(selectedstring.toArray().join(", ")); }); }); </script> </head> <body> <form id="form1" runat="server"> <div class="container"> <asp:CheckBox ID="CheckBox1" runat="server" Text="Text 1" /> <asp:CheckBox ID="CheckBox2" runat="server" Text="Text 2" /> <asp:CheckBox ID="CheckBox3" runat="server" Text="Text 3" /> <asp:CheckBox ID="CheckBox4" runat="server" Text="Text 4" /> <asp:CheckBox ID="CheckBox5" runat="server" Text="Text 5" /> <asp:CheckBox ID="CheckBox6" runat="server" Text="Text 6" /> <asp:CheckBox ID="CheckBox7" runat="server" Text="Text 7" /> <asp:CheckBox ID="CheckBox8" runat="server" Text="Text 8" /> <asp:CheckBox ID="CheckBox9" runat="server" Text="Text 9" /> <asp:CheckBox ID="CheckBox10" runat="server" Text="Text 10" /> <asp:CheckBox ID="CheckBox11" runat="server" Text="Text 11" /> <asp:CheckBox ID="CheckBox12" runat="server" Text="Text 12" /> <asp:CheckBox ID="CheckBox13" runat="server" Text="Text 13" /> <asp:CheckBox ID="CheckBox14" runat="server" Text="Text 14" /> <asp:CheckBox ID="CheckBox15" runat="server" Text="Text 15" /> <asp:CheckBox ID="CheckBox16" runat="server" Text="Text 16" /> <asp:CheckBox ID="CheckBox17" runat="server" Text="Text 17" /> <asp:CheckBox ID="CheckBox18" runat="server" Text="Text 18" /> </div> <input type="button" id="clearselection" value="clear selection" /> <input type="button" id="getconcanated" value="get concanated" /> </form> </body> </html>Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog