Asp.net中使用Server.HtmlDecode(string str)的使用

前言:

在使用Visual Studio开发web页面时,需要在GridView中绑定Table数据,并加入了CommandField,

试图,点击详情按钮是,获取GridView中Rows中Cells[1]的值,我使用了如下语句,如: string cart = GridViewPacked.Rows[e.NewSelectedIndex].Cells[1].Text.ToString();

并使用了,Session["cart"] = cart;存入缓存。

今天突然发现当GridView中Cart单元格为T2 S&V(VI)-15B0001时导致获取到的String cart=‘T2 S&V(VI)-15B0001’

经过查资料,知道是HtmlEncode引起的。

解决办法:

1、网上很多都是在GridView绑定数据之前,进行加入資料行,再设定其HtmEncode=False,

以上方法,需要逐个增加GridView中的列,并设定其HtmlEncode属性,对非固定列的Table绑定时无法使用。

2、在GridView的RowDataBound事件(该事件在行数据绑定完成后触发)中加入Server.HtmlDecode(string str)函數即可

如下:

        if (e.Row.RowType == DataControlRowType.DataRow)    

     {

            TableCellCollection cells = e.Row.Cells;  

           for (int i = 1; i < cells.Count; i++)     

        {           

            TableCell cell = cells[i];        

            cell.Text = Server.HtmlDecode(cell.Text);  

         }     

     }

   

posted @ 2015-11-30 16:39  Galen会幸福  阅读(561)  评论(0编辑  收藏  举报