从 Web 部件中的列表属性呈现 HTML

发布于 2024-12-14 07:46:58 字数 566 浏览 0 评论 0原文

我正在尝试从 SharePoint 列表中读取一些值并将它们以 HTML 形式呈现在我的页面中。但是,它按字面意思显示 Web 部件页面上的 HTML 标记,但无法呈现它。

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
  writer.WriteLine("<br />"); // this works
  writer.WriteLine(htbl["key"].ToString()); // html fails to render. literally renders the HTML in the string
}

我已经看到了以下方法:

  1. 文本到 HTML – 提到的 JavaScript在我看来似乎有点矫枉过正。难道就没有更简单的方法来实现这一点吗?

请指教。

I am trying to read some values from a SharePoint List and render them in my page as HTML. However, it literally shows the HTML tags on the Web Part page and fails to render it.

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
  writer.WriteLine("<br />"); // this works
  writer.WriteLine(htbl["key"].ToString()); // html fails to render. literally renders the HTML in the string
}

I have seen following approaches already:

  1. Text to HTML – the JavaScript mentioned seems to be overkill in my opinion. Is there no easier way to accomplish this?

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-12-21 07:46:58

克里斯,

你在一行文本中到底放了什么?

我尝试了以下方法,并且没有任何问题。如果您将此与您的 Webpart 类进行比较,您发现差异了吗?

   using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;   

 namespace test.WebPart1
    {
        [ToolboxItemAttribute(false)]
        public class WebPart1 : WebPart
        {
            protected override void CreateChildControls()
            {

            }

            protected override void Render(HtmlTextWriter writer)
            {
                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists["testlist"];
                foreach (SPListItem item in list.Items)
                {
                    writer.Write(item["testcolumn"].ToString());
                }
            }
        }
    }

Kris,

what do you put in the single line of text exactly?

I tried the following and this worked without any issues. If you compare this with your webpart class, do you see differences?

   using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;   

 namespace test.WebPart1
    {
        [ToolboxItemAttribute(false)]
        public class WebPart1 : WebPart
        {
            protected override void CreateChildControls()
            {

            }

            protected override void Render(HtmlTextWriter writer)
            {
                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists["testlist"];
                foreach (SPListItem item in list.Items)
                {
                    writer.Write(item["testcolumn"].ToString());
                }
            }
        }
    }
乜一 2024-12-21 07:46:58

原因是该列是多行文本,并且允许的文本类型设置为“富文本(粗体、斜体、文本对齐)”,因此无法正确呈现 HTML。解决方案是将其更改为纯文本。

The reason was that the column was a multiple line of text and the type of text allowed was set to "Rich text (bold, italics, text alignment)", thus not making it possible to render the HTML properly. The resolution is to change it to plain text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文