Reputation: 11
I have a form with a check box list in it. The check box list is databound to a table in the database. I need to set a default value for the check box when the pages loads and when I click a clear button to clear all the text boxes etc, that are on the form. Can someone tell me how to o that?
Upvotes: 0
Views: 3079
Reputation: 457
//Check particular checkbox
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Text.Trim() == "C#")//text or value (item.Value.Trim())
{
item.Selected = true;
}
}
//check all checkbox
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}
//uncheck
protected void Button1_Click(object sender, EventArgs e)
{
foreach(ListItem li in CheckBoxList1.Items)
{
li.Selected = false;
}
}
Using jQuery clear all the inputs of form
<script type="text/javascript">
$("#Button1").click(function() {
$('#form1').find(':input').each(function() {
switch (this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
});
</script>
Upvotes: 1