[英]Gridview get Checkbox.Checked value
I have a GridView that has 10 columns populated by CheckBoxes. 我有一个GridView,其中有10列由CheckBoxes填充。 But instead of using
FindControl()
is there a way to get the CheckBox.Checked
value by using a loop? 但是除了使用
FindControl()
,还有一种方法可以通过循环来获取CheckBox.Checked
值?
Current Code: 当前代码:
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 CODE: ASPX代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="101">
<ItemTemplate>
<asp:CheckBox runat="server" id="cb101" AutoPostBack="false" Checked='<%# Eval("101").ToString()=="1" ? true : false %>' Enabled='<%#(String.IsNullOrEmpty(Eval("101").ToString()) ? false: true) %>'/>
</ItemTemplate>
</asp:TemplateField>
....and so on
<asp:ButtonField ButtonType="Button" CommandName="updaterow" Text="Update"/>
</Columns>
</asp:GridView>
Try this, 尝试这个,
Using foreach
Loop: 使用
foreach
循环:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
Use it in OnRowCommand
event and get checked CheckBox value. 在
OnRowCommand
事件中使用它并获取选中的CheckBox值。
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];
For run all lines of GridView don't use for
loop, use foreach
loop like: 为了运行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
}
}
}
You want an independent for loop for all the rows in grid view, then refer the below link 您希望为网格视图中的所有行提供独立的for循环,然后参考以下链接
http://nikhilsreeni.wordpress.com/asp-net/checkbox/ http://nikhilsreeni.wordpress.com/asp-net/checkbox/
Select all checkbox in Gridview 选中Gridview中的所有复选框
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);
}
}
If you want a method other than findcontrol try the following: 如果您需要除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]);
}
Blockquote
块引用
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(" ", "");
}
}
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1");
if (chkbox.Checked == true)
{
// Your Code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.