views:

7764

answers:

5

Hi,I have used checkbox column in gridview. I want to check status of that checkboxes. On click of a button it should be checked that if any checkbox is checked or not. If none checkbox is checked then it should display alert message that check checkbox first. I need it urgently. Can anyone help me please? Thanks in advance.

+2  A: 
if(document.getElementById('checkBoxId').checked) {
    //checked
} else {
    //not checked
}

edit: if you want to check all checkboxes of a form you can loop through the collection:

var inputs = document.getElementById('formId').getElementsByTagName('input');
var isChecked = false
for( var i = 0; i < inputs.length; i++) {
    if(inputs[i].type == 'checkbox' && inputs[i].checked) {
        isChecked = true;
    }
}

if(isChecked) {
    //at least one checkbox checked
}
I.devries
thanx, but checkboxes are in gridview item template. so it can not be accessed as gridview is converted in table format in html.
Devashri
+2  A: 

Server side:

//in your button click event :

bool flag = false;

for( int i=0; i < gridview1.rows.count ; i++)

{
if(checkbox1.checked)

 flag = true;

}

if(flag)

{

//means atleast one check box is checked

}

Samiksha
Thanks for answer. Buty I have to do it with java script.
Devashri
+1  A: 

Hey, I found answer. It is as follows:

function checkBoxselectedornot()
{

       var frm=document.forms['aspnetForm'];
       var flag=false;
       for(var i=0;i<document.forms[0].length;i++)
       {
           if(document.forms[0].elements[i].id.indexOf('chkDownloadSelectedEvent')!=-1)
           {
                 if(document.forms[0].elements[i].checked)
                 {
                      flag=true
                 }  
           }
       } 
      if (flag==true)
      {
        return true
      }else
      {
        alert('Please select at least one Event.')
        return false
      }

}
Devashri
+1  A: 

You will have to add some custom Javascript to your page for the client-side alert to show. Here's a method that I've written that works with a GridView called 'GridView1' (this should be the default name if you've just dragged the control onto your ASPX page):

<script type="text/javascript">
    function ClientCheck() {
        var valid = false;
        var gv = document.getElementById("GridView1");

        for (var i = 0; i < gv.all.length; i++) {
            var node = gv.all[i];
            if (node != null && node.type == "checkbox" && node.checked) {
                valid = true;
                break;
            }
        }
        if (!valid) {
            alert("Invalid. Please select a checkbox to continue.");
        }

        return valid;
    }
</script>

You can see that it sets a variable to the GridView control to start with, then iterates through all the children in a for loop. If the child is a checkbox and it is checked, then we set the valid variable to true. If we get to the end of the iteration and no checked checkboxes are found, then valid remains false and we execute the alert.

To link this into your GridView on your ASPX page, first make the button column a TemplateField and surround the LinkButton with your client-side code. If you've used the designer to set up your columns, you can use the "Convert this field into a TemplateField" link in the column editor). Here's an example of the source you'll end up with:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
    <Columns>
        <asp:TemplateField HeaderText="Button Field" ShowHeader="False">
            <ItemTemplate>
                <span onclick="return ClientCheck();">
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="IDClick" Text='<%# Eval("YourDataSourceItem") %>'></asp:LinkButton>
                </span>
            </ItemTemplate>
        </asp:TemplateField>
        // ...your remaining columns...

Using the TemplateField lets us add any client-side code we like. Here we're adding a span and using onclick to call our ClientCheck method.

If you aren't bothered about the alert, you could achieve your aims by using a CustomValidator control, which executes on the server-side.

I hope this helps.

Dave R.
A: 
function CheckboxSelect() { var LIntCtr; var LIntSelectedCheckBoxes = 0; for (LIntCtr = 0; LIntCtr -1)) { if (document.forms[0].elements[LIntCtr].checked == true) { LIntSelectedCheckBoxes = parseInt(LIntSelectedCheckBoxes) + 1; } } } if (parseInt(LIntSelectedCheckBoxes) == 0) { alert('User(s) Must Be Selected For operation !'); return false; } }
Amit