admin管理员组文章数量:1225483
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 badges6 Answers
Reset to default 4Hey, 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>
本文标签: chow to check status of checkboxes in gridview columns on click of buttonStack Overflow
版权声明:本文标题:c# - how to check status of checkboxes in gridview columns on click of button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739459358a2164073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论