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);  

         }     

     }

相關文章
相關標籤/搜索