asp.net中應用Extjs的grid不顯示HTML內容

來源:互聯網
上載者:User

最近用asp.net結合Extjs開發一個新聞發布功能的模組,發現Extjs的grid無法顯示帶Html標記的新聞內容,百思不得其解,在FF和IE下沒有任何錯誤,而且,FF調試下,新聞內容資料已經取出來了,而且json格式也是正確的,但就是不顯示。

 

很奇怪!

最終經過反覆研究,發現一個細節,我們從網頁直接拷貝新聞,然後粘貼到Extjs的htmleditor裡,發布,會含有很多的\r   \n 標記,只要卻掉就OK了,而且\是逸出字元,要做處理,不然同樣不顯示的。

json格式雖然簡單,但是一定要組織好,不然就不會顯示了。

好說解決方案:

很簡單,只要將新聞內容欄位做一個replace就可以了。看我的IList2json的方法:

public string ToJsonString<T>(string jsonName, IList IL)
    {
        StringBuilder Json = new StringBuilder();
        Json.Append("{" + jsonName + ":[");
        if (IL.Count > 0)
        {
            for (int i = 0; i < IL.Count; i++)
            {
                T obj = Activator.CreateInstance<T>();
                Type type = obj.GetType();
                PropertyInfo[] pis = type.GetProperties();
                Json.Append("{");
                for (int j = 0; j < pis.Length; j++)
                {
                    string ILValue = "";
                    if (pis[j].GetValue(IL[i], null) != null)
                    {
                        ILValue = pis[j].GetValue(IL[i], null).ToString().Replace("'", "\"").Replace("\r", "").Replace("\n", "").Replace("\\

',"////");//關鍵就是這裡,替換掉敏感字元就OK了
                    }
                    Json.Append("'" + pis[j].Name.ToString() + "':'" + ILValue + "'");
                    if (j < pis.Length - 1)
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("}");
                if (i < IL.Count - 1)
                {
                    Json.Append(",");
                }
            }
        }
        Json.Append("]}");
        return Json.ToString();
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.