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

前言:web

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

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

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

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

通过查资料,知道是HtmlEncode引发的。事件

解决办法:开发

一、网上不少都是在GridView绑定数据以前,进行加入資料行,再设定其HtmEncode=False,string

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

二、在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);  

         }     

     }

相关文章
相关标签/搜索