I am use Visual Studio 2008 to coding ASP.NET in C#
I am trying to get value checked or unchecked form checkbox in Gridview. This is my code to do.
void imbTransferBySelect_Click(object sender, ImageClickEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("Transfer_Selector");
if (cb.Checked)
{
//do something
}
}
}
When the checkbox is unchecked the code runs fine.
But when checkbox is checked value of GridView1.Rows.Count = 0 it's make for loop is fail
I try use another code in http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("Transfer_Selector") as CheckBox);
if (chkRow.Checked)
{
//do something
}
}
}
It's runs fine when check-box is not checked.
And when check-box checked it's make loop in for-each fail again.
Anyone ever had a problem like me or know what's happened.
CheckBox?