C# 使用复选框在 ASP.NET GridView 中选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1127579/
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
Using a CheckBox to Select Rows in an ASP.NET GridView
提问by flavour404
With a GridView
control in .NET is there any way to change the Select
option in a GridView
to a Checkbox
, how do you select multiple rows?
使用GridView
.NET 中的控件有什么方法可以将 a 中的Select
选项更改GridView
为 a Checkbox
,如何选择多行?
I tried adding a CheckBoxField
into my GridView
but when I ran it it didn't show up.
我尝试CheckBoxField
在我的中添加一个,GridView
但是当我运行它时它没有出现。
采纳答案by BinaryHacker
Adding checkbox to Gridview is as simple as adding TemplateField of any control.
将复选框添加到 Gridview 就像添加任何控件的 TemplateField 一样简单。
I tried adding a CheckBoxField into my gridview but when I ran it it didn't show up.
我尝试将 CheckBoxField 添加到我的 gridview 中,但是当我运行它时它没有出现。
There should be other columns binding data to the grid. Also, check visible property of checkbox.
应该有其他列将数据绑定到网格。另外,检查复选框的可见属性。
Refer to this link for more on this. link text
有关更多信息,请参阅此链接。链接文字
回答by JBrooks
I always just add a column to the DataTable that I am binding to.
我总是只在我绑定到的 DataTable 中添加一列。
dt.Columns.Add(new DataColumn("Include", typeof(Boolean)));
Or in my SQL I will have:
或者在我的 SQL 中,我将有:
declare @include bit
set @include = 0
select
@include Include,
....
More can be found on my blog here
更多可以在我的博客上找到
回答by flavour404
I did work it out in the end, thanks to ExpertSoul for the headsup on the tutorial. This was all I needed in the markup and it worked great:
最后我确实解决了这个问题,感谢 ExpertSoul 对教程的提示。这就是我在标记中所需要的一切,而且效果很好:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="PublicationSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
The other thing that the tutorial had which was great was an onclick event so that you could pick up the id # when the submit button was clicked, which was the next bit...
本教程的另一件很棒的事情是 onclick 事件,这样您就可以在单击提交按钮时获取 id #,这是下一点......
Great stuff.
好东西。