利用GridView的DataKeyNames属性,替代HiddenField。如果你已经应用了以前的方法,你可以保留,再写一个方法重载。
重构之后:
{
string strTempValue = string.Empty;
foreach (GridViewRow gvr in gridView.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl(checkBoxId);
if (cb.Checked)
{
strTempValue = strTempValue + "," + gridView.DataKeys[gvr.RowIndex].Value.ToString();
}
}
return strTempValue;
}