此篇是修正http://www.cnblogs.com/insus/articles/1413740.html。
修正的地方,就是利用GridView的DataKeyNames属性,替代HiddenField。如果你已经应用了以前的方法,你可以保留,再写一个方法重载。
View Code
<asp:GridView ID="GridView1" runat="server" DataKeyNames="xxx"
重构之后:
View Code
string GetCheckBoxSelectValue(GridView gridView, string checkBoxId)
{
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;
}
{
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;
}