最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - how to check status of checkboxes in gridview columns on click of button - Stack Overflow

programmeradmin0浏览0评论

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 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.

Share Improve this question edited Dec 13, 2017 at 10:59 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Dec 8, 2008 at 10:25 Devashri B.Devashri B. 2,7038 gold badges28 silver badges40 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 4

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
      }

}
   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;
            }      
        }
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
}

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

}

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.

 <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>
发布评论

评论列表(0)

  1. 暂无评论