本文介绍了如何检查网格内的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的MVC项目中我有一个页面。在我的dropdowncontrol值为USER和ADMIN 在我的网格中我有三行 ALLOW ACTION DELETE [CHECKBOX] INSERT [CHECKBOX] UPDATE [CHECKBOX] 如果我在下拉列表中选择USER,我想检查INSERT复选框。 如果我选择ADMIN,我会检查所有Checkbox 我该怎么做? 先谢谢 Kunjammu 解决方案 参考以下链接 http://stackoverflow.com/questions/19628554/gridview-get-checkbox-checked-value [ ^ ] http://stackoverflow.com/questions/20452844/how-to-check-if-datagridview-复选框已选中 [ ^ ] http://www.aspsnippets.com/Articles/GridView-with-CheckBox -Get-Selected-Rows-in-ASPNet.aspx [ ^ ] 如何在c#中将复选框列添加到gridview [ ^ ] http ://forums.asp.net/t/1185094.aspx?如何+ +检查+如果+ CheckBox +是+ + + + + + + + [ ^ ] HTTP:// WWW。 c-sharpcorner.com/Forums/Thread/217846/how-to-get-checked-value-of-dynamic-checkbox-inside-gridview.aspx [ ^ ] < text> 嗨朋友们,请参阅以下代码。 此代码仅包含一个复选框。使用此参考,您可以在网格中添加三个CheckBox。 < text> HTML标记 < asp:GridView ID = GridView1 runat = server AutoGenerateColumns = false > < 列 > < asp:Templat eField > < ItemTemplate > < asp:CheckBox ID = CheckBox1 runat = server 文本 =' <% #Eval( Id)%>' / > < / ItemTemplate > ; < / asp:TemplateField > < asp:BoundField DataField = 名称 HeaderText = 名称 / > < asp:BoundField DataField = 国家/地区 HeaderText = 国家/地区 / > < /列 > < / asp:GridView > < asp:CustomValidator ID = CustomValidator1 runat = server ErrorMessage = 请选择至少一条记录。 ClientValidationFunction = 验证 ForeColor = 红色 > < / asp:CustomValidator > < br / > < asp:按钮 ID = Button1 runat = server 文本 = 提交 / > < text> 绑定GridView控件 受保护 无效 Page_Load( object sender,EventArgs e) { if (!this。 IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange( new DataColumn [ 3 ] { new DataColumn( Id), new DataColumn( 名称), new DataColumn( Country)}); dt.Rows.Add( 1 , John Hammond, 美国); dt.Rows.Add( 2 , Mudassar Khan, India); dt.Rows.Add( 3 , Suzanne Mathews, France); dt.Rows.Add( 4 , Robert Schidner, 俄罗斯); GridView1.DataSource = dt; GridView1.DataBind(); } } < text> CustomValidator JavaScript验证功能 < script type = text / javascript> function 验证(sender,args){ var gridView = document .getElementById( <%= GridView1.ClientID%> ); var checkBoxes = gridView.getElementsByTagName( 输入); for ( var i = 0 ; i< checkBoxes.length; i ++){ if (checkBoxes [i] .type == 复选框&& checkBoxes [i] .checked){ args.IsValid = 真; return ; } } args.IsValid = false ; } < / script> < / script> Hi,In my MVC Project i have a page. in that i have dropdowncontrol with values USER and ADMINin my grid i have three rowsALLOW ACTIONDELETE [CHECKBOX]INSERT [CHECKBOX]UPDATE [CHECKBOX]if i select USER in dropdownlist, i want check INSERT checkbox.if i select ADMIN , i wnat to check all CheckboxHow can i do that?Thanks in AdvanceKunjammu 解决方案 refer following linkshttp://stackoverflow.com/questions/19628554/gridview-get-checkbox-checked-value[^]http://stackoverflow.com/questions/20452844/how-to-check-if-datagridview-checkbox-is-checked[^]http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx[^]How to add checkbox column to gridview in c#[^]http://forums.asp.net/t/1185094.aspx?How+to+check+if+CheckBox+is+check+in+C+[^]http://www.c-sharpcorner.com/Forums/Thread/217846/how-to-get-checked-value-of-dynamic-checkbox-inside-gridview.aspx[^]<text>Hi Friends, Refer Following Code.This Code Contains only one Checkbox. Using this Reference you can Add Three CheckBox in your Grid.<text>HTML Markup<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Id") %>' /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Country" HeaderText="Country" /> </Columns></asp:GridView><asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one record." ClientValidationFunction="Validate" ForeColor="Red"></asp:CustomValidator><br /><asp:Button ID="Button1" runat="server" Text="Submit" /><text>Binding the GridView controlprotected void Page_Load(object sender, EventArgs e){ if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") }); dt.Rows.Add(1, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(3, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); }}<text>CustomValidator JavaScript Validation function<script type="text/javascript"> function Validate(sender, args) { var gridView = document.getElementById("<%=GridView1.ClientID %>"); var checkBoxes = gridView.getElementsByTagName("input"); for (var i = 0; i < checkBoxes.length; i++) { if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) { args.IsValid = true; return; } } args.IsValid = false; }</script></script> 这篇关于如何检查网格内的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 13:57