jeudi 12 février 2015

GridView with Checkbox in ASP.NET

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://ift.tt/1FBt6Jy



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.





Aucun commentaire:

Enregistrer un commentaire