Gridview获取Checkbox.Checked值
我有一个GridView,其中有10个列由CheckBoxes填充。 但是使用FindControl()
是否有办法通过循环获取CheckBox.Checked
值?
现行代码:
if (e.CommandName == "updaterow") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow selectedRow = GridView1.Rows[index]; // TableCell BranchCode = selectedRow.Cells[0]; CheckBox cb101 = (CheckBox)selectedRow.FindControl("cb101"); CheckBox cb102 = (CheckBox)selectedRow.FindControl("cb102"); //...and so on }
ASPX代码:
<asp:CheckBox runat="server" id="cb101" AutoPostBack="false" Checked='' Enabled=''/> ....and so on
试试这个,
使用foreach
循环:
foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = row.Cells[0].Controls[0] as CheckBox; if (chk != null && chk.Checked) { // ... } }
在OnRowCommand
事件中使用它并获取CheckBox值。
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer); int requisitionId = Convert.ToInt32(e.CommandArgument); CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];
对于运行GridView的所有行都不使用for
循环,使用foreach
循环如:
foreach (GridViewRow row in yourGridName.Rows) //Running all lines of grid { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox); if (chkRow.Checked) { //if checked do something } } }
如果你想要一个findcontrol以外的方法,请尝试以下方法:
GridViewRow row = Gridview1.SelectedRow; int CustomerId = int.parse(row.Cells[0].Text);// to get the column value CheckBox checkbox1= row.Cells[0].Controls[0] as CheckBox; // you can access the controls like this
foreach (DataRow row in DataRow row in GridView1.Rows) { foreach (DataColumn c in GridView1.Columns) bool ckbVal = (bool)(row[c.ColumnName]); }
大段引用
foreach (GridViewRow row in tempGrid.Rows) { dt.Rows.Add(); for (int i = 0; i < row.Controls.Count; i++) { Control control = row.Controls[i]; if (control.Controls.Count==1) { CheckBox chk = row.Cells[i].Controls[0] as CheckBox; if (chk != null && chk.Checked) { dt.Rows[dt.Rows.Count - 1][i] = "True"; } else dt.Rows[dt.Rows.Count - 1][i] = "False"; } else dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text.Replace(" ", ""); } }
您希望网格视图中的所有行都有一个独立的for循环,然后参考下面的链接
选中Gridview中的所有复选框
上述就是C#学习教程:Gridview获取Checkbox.Checked值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
CheckBox cb = default(CheckBox); for (int i = 0; i <= grdforumcomments.Rows.Count – 1; i++) { cb = (CheckBox)grdforumcomments.Rows[i].Cells[0].FindControl(“cbSel”); cb.Checked = ((CheckBox)sender).Checked; } Select checked rows to a dataset; For gridview multiple edit CheckBox cb = default(CheckBox); foreach (GridViewRow row in grdforumcomments.Rows) { cb = (CheckBox)row.FindControl("cbsel"); if (cb.Checked) { drArticleCommentsUpdates = dtArticleCommentsUpdates.NewRow(); drArticleCommentsUpdates["Id"] = dgItem.Cells[0].Text; drArticleCommentsUpdates["Date"] = System.DateTime.Now;dtArticleCommentsUpdates.Rows.Add(drArticleCommentsUpdates); } }
foreach (GridViewRow row in GridView1.Rows) { CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1"); if (chkbox.Checked == true) { // Your Code } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1007238.html