ASP.NET - Get selected CheckBox value in DataList
Introduction
A while ago, someone at the forum had asked "how to retrieve the checkbox value from the ASP.NET DataList control". I took the opportunity to put together a sample code to achieve the requirement through the Code Behind and jQuery.
In this example, I'm using HTML Input (CheckBox
) control in the DataList
. Shown in listing 1 is the code used to get the selected checkbox value. The code will loop through the DataListItemCollection
, find the CheckBox
control by ID, then do something if it was checked.
void GetCheckedBox()
{
foreach (DataListItem li in BooksDataList.Items)
{
HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;
if (cb != null)
{
if (cb.Checked)
{
if (LblText.Text.Length > 0)
LblText.Text += ", ";
LblText.Text += cb.Value;
}
}
}
}
Let's say we decide not to write code to loop through the DataListItemCollection
, we can collect the selected CheckBox
value through the client-side script. Shown in Listing 2 is the jQuery script used to collect the selected CheckBox
value. When the user hits the submit button, the script will retrieve all the CheckBox
values in the DataList
control, stored in an array and later save it to a hidden field.
<script type="text/javascript">
$(document).ready(function () {
//check all
$("[id$='chkAll']").click(
function () {
$("INPUT[type='checkbox']").attr('checked',
$("[id$='chkAll']").is(':checked'));
});
$("[id$='btnSubmit']").click(function () {
var ISBN = [];
$("[id$='BooksDataList']
input[type=checkbox]:checked").each(function () {
ISBN.push($(this).val());
});
$("[id$='HiddenField1']").val(ISBN);
});
});
</script>
In the code behind, we can retrieve the hidden field value like this "HiddenField1.Value;
"
Conclusion
I hope someone will find this information useful and that it will make your programming job easier. If you find any bugs or disagree with the contents or want to help improve this article, please drop me a line and I'll work with you to correct it. I would suggest downloading the demo and exploring it in order to grasp the full concept of it because I might miss some important information in this article. Please send me an email if you want to help improve this article.
Watch This Script in Action
Post Comment
cmrkpj Major thankies for the blog post.Really looking forward to read more. Will read on...
OmEnyI Thank you ever so for you article post.Really looking forward to read more. Keep writing.
белорусские казино, либо игры азартные на раздевание бесплатно.
LV3Vdy I appreciate you sharing this blog post. Much obliged.
VLILte I think this is a real great blog.Really thank you! Much obliged.
Admiring the dedication you put into your blog and in depth information you present. It's good to come across a blog every once in a while that isn't the same old rehashed material. Wonderful read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.
Hey there! I've been following your weblog for a long time now and finally got the courage to go ahead and give you a shout out from Houston Tx! Just wanted to tell you keep up the great job!
Howdy! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!
I have been examinating out a few of your stories and its nice stuff. I will definitely bookmark your blog.
At this time improvements in could also be seen in a point of contention.
Heya! It seems as though we both have a interest for the same thing. Your blog, "%BLOGTITLE%" and mine are very similar. Have you ever thought about writing a guest write-up for a related blog? It will definitely help gain publicity to your website (my site recieves a lot of targeted traffic). If you might be interested, email me at: %EMAIL%. Thanks for your time
Blasphemy! LOL Just kidding. Ive read similar things on other blogs. Ill take your word for it. Stay solid! your pal.
really great stuff
Tyvm for the helpful post! I would not have gotten this myself!
A client may ever be in the spy of morality but module ever rise above a cats glossa
Bom dia amigos, verdadeiramente mandar um SMS grátis está cada vez mais custoso devido falta de op??es das operadoras móveis. Há ainda os websites que prometem entregar minha menssagem mas nem sempre chegam ao usuário final. Alguns como o Mundo oi e o Oi Torpedo funcionam mas e para as outras operadoras? E os que prometem que enviam e nada chega. Para onde está indo as meus torpedos? E para a Tim, Vivo? Alguma Idéia? Ou significa ter de desembolsar?. ? só um desabafo, realmente está difícil achar servi?os para enviar SMS barato.