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

T used checkbox column in gridview. When clicking the link button, check if the checkboxes in the gridview are checked or not. If no checkbox is checked, then it should display a warning ("Check at leat one check box").

+1


source to share


4 answers


I found the answer. and his worker ...

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
      }

}

      

and the girdview code ...



          

            <asp:BoundField ItemStyle-Width ="100px" DataField ="EventStartDate" DataFormatString ="{0:g}" HeaderText ="<%$ Resources:stringsRes, ctl_EventList_StartDate %>" SortExpression ="EventStartDate" HtmlEncode ="true" ItemStyle-Wrap ="false" />
            <asp:BoundField ItemStyle-Width="100px" DataField="EventDate" DataFormatString="{0:g}" HeaderText="<%$ Resources:stringsRes, ctl_EventList_Date %>"  SortExpression="EventDate" HtmlEncode="true" ItemStyle-Wrap="false" />
            <asp:ButtonField ItemStyle-Width="150px" ButtonType="Link" DataTextField="EventName" HeaderText="<%$ Resources:stringsRes, ctl_EventList_Event %>"  SortExpression="EventName" CommandName="show_details" CausesValidation="false" />
            <asp:BoundField DataField="EventLocation" HeaderText="<%$ Resources:stringsRes, ctl_EventList_Location %>" SortExpression="EventLocation" />
            <asp:TemplateField HeaderText ="Select">
            <ItemTemplate >
                <asp:CheckBox ID="chkDownloadSelectedEvent" runat ="server" AutoPostBack ="false" Onclick="eachCheck();"/>                   

            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle Height="25px" />
        <HeaderStyle Height="30px"/>
    </asp:GridView>

      

and there is a link button ....

+1


source


You will need to add custom Javascript to your page for client side notification. Here's a method I wrote that works with a GridView called "GridView1" (this should be the default name if you just dragged the control onto the 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 control GridView

to begin with, then iterates over all the children in the loop for

. If the child has a value checkbox

and is checked

, we set the variable valid

to true. If we reach the end of the iteration and do not check the boxes, it valid

will remain false and we will execute the warning.

To tie this to yours GridView

in your ASPX page, first make a column of buttons TemplateField

and surround with LinkButton

your client side code. If you've used the designer to customize your columns, you can use the Convert this field to TemplateField link in the column editor). Here's an example of the source you 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...

      

Usage TemplateField

allows us to add whatever client code we like. Here we add span

and use onclick

to call our method ClientCheck

.

If you're not worried about the warning, you can achieve your goals by using a control CustomValidator

that runs on the server side.

Hope this helps.

+1


source


I havnt used a checkbox as a grid, but wouldn’t do a for loop around the columns in the gridview and check the state? Myabe adds a counter, and if 0, then a warning.

0


source


var grid = document.getElementById("gridId");  //Retrieve the grid    
    var inputs = grid.getElementsByTagName("input"); //Retrieve all the input elements from the grid
    var isValid = false;
    for (var i=0; i < inputs.length; i += 1) { //Iterate over every input element retrieved
        if (inputs[i].type === "checkbox") { //If the current element type is checkbox, then it is wat we need
            if(inputs[i].checked === true) { //If the current checkbox is true, then atleast one checkbox is ticked, so break the loop
                isValid = true;
                break;
            }
        }
    }
    if(!isValid) {
        alert('Check at least one checkbox');
    }

      

0


source







All Articles