Devashri B.
Devashri B.

Reputation: 2703

how to check status of checkboxes in gridview columns on click of button

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.

Upvotes: 3

Views: 22568

Answers (6)

Amit
Amit

Reputation: 1

 <script type="text/javascript" language="javascript">
        function CheckboxSelect() {

            var LIntCtr;
            var LIntSelectedCheckBoxes = 0;

            for (LIntCtr = 0; LIntCtr < document.forms[0].elements.length; LIntCtr++) {
                if ((document.forms[0].elements[LIntCtr].type == 'checkbox') && (document.forms[0].elements[LIntCtr].name.indexOf('chkID') > -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;
            }
        }
    </script>

Upvotes: 0

Ronak
Ronak

Reputation: 61

   protected void OnCheckedChanged(object sender, EventArgs e)
        {
            bool flag = false;

            foreach (GridViewRow row in Grid_InvoiceGarden.Rows)
            {
                CheckBox chkItem = (CheckBox)row.FindControl("chkSelect");
                if (chkItem.Checked)
                    flag = true;
            }
            if (flag == true)
            {
                btnUpdate.Visible = true;
            }
            else
            {
                btnUpdate.Visible = false;
            }      
        }

Upvotes: 3

Devashri B.
Devashri B.

Reputation: 2703

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
      }

}

Upvotes: 4

Dave R.
Dave R.

Reputation: 7304

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.

Upvotes: 1

Samiksha
Samiksha

Reputation: 6182

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

}

Upvotes: 2

I.devries
I.devries

Reputation: 8916

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
}

Upvotes: 2

Related Questions