C# 如何通过单击按钮检查 gridview 列中复选框的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/349055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check status of checkboxes in gridview columns on click of button
提问by Devashri B.
T have used checkbox column in gridview. On click of a linkbutton, it should be checked that checkboxes in gridview are checked or not. If none check box is checked then it should display alert("Check at leat one check box").
在 gridview 中使用了复选框列。单击链接按钮时,应检查 gridview 中的复选框是否已选中。如果未选中任何复选框,则应显示警报(“至少选中一个复选框”)。
采纳答案by Devashri B.
I found the answer. and its working...
我找到了答案。和它的工作...
function checkBoxselectedornot() {
函数 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 girdview code is...
和 girdview 代码是...
<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 ....
并且有一个链接按钮....
回答by xoxo
I havnt used the checkbox in grid view but would you not do a for loop around the columns in gridview and check the state? Myabe add a count and if 0 then alert.
我没有在网格视图中使用复选框,但是您不会在 gridview 中的列周围进行 for 循环并检查状态吗?Myabe 添加一个计数,如果为 0,则警报。
回答by Andreas Grech
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's 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');
}
回答by Dave R.
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):
您必须向页面添加一些自定义 Javascript 才能显示客户端警报。这是我编写的一个方法,它与名为“GridView1”的 GridView 一起使用(如果您刚刚将控件拖到 ASPX 页面上,这应该是默认名称):
<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.
您可以看到它为GridView
控件设置了一个变量以开始,然后循环遍历所有子项for
。如果孩子是 acheckbox
并且它是checked
,那么我们将valid
变量设置为 true。如果我们到达迭代结束并且没有找到选中的复选框,则valid
保持 false 并且我们执行警报。
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:
要将其链接到您GridView
的 ASPX 页面上,首先将按钮列设为aTemplateField
并LinkButton
用您的客户端代码包围。如果您使用设计器来设置列,则可以使用列编辑器中的“将此字段转换为模板字段”链接)。这是您最终将获得的源示例:
<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.
使用TemplateField
可以让我们添加任何我们喜欢的客户端代码。在这里,我们添加了span
和 usingonclick
来调用我们的ClientCheck
方法。
If you aren't bothered about the alert, you could achieve your aims by using a CustomValidator
control, which executes on the server-side.
如果您不担心警报,您可以通过使用CustomValidator
在服务器端执行的控件来实现您的目标。
I hope this helps.
我希望这有帮助。