把选中Gridview的记录显示出来 Ver2

此篇是修正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;
        }

 

posted @ 2011-11-07 10:25  Insus.NET  阅读(2117)  评论(1编辑  收藏  举报