.Net 函数替换/编码和解码字符串中的 HTML 字符

发布于 2024-09-12 03:20:36 字数 1281 浏览 11 评论 0原文

我有以下代码:

  Dim lStatementText As String
  Dim lStatementString As New System.Text.StringBuilder

  lStatementString.Append(RndRes.Forms.txt_request)
  lStatementString.Append(" ")
  lStatementString.Append("<b><a>")
  lStatementString.Append(Request.ID)
  lStatementString.Append("</a> - <a>")
  lStatementString.Append(Request.Description)
  lStatementString.Append("</a></b> ")
  lStatementString.Append(RndRes.Forms.txt_IsNotYetLoaded)
  lStatementString.Append(". ")
  lStatementString.Append(RndRes.Forms.txt_click)
  lStatementString.Append(" <b><a>")
  lStatementString.Append(RndRes.Forms.txt_here)
  lStatementString.Append(" </a></b> ")
  lStatementString.Append(RndRes.Forms.txt_GetFromDB)

  lStatementText = lStatementString.ToString()

  Dim lLink As New Infragistics.Win.FormattedLinkLabel.UltraFormattedLinkLabel()
  lLink.Value = lStatementText
  lLink.TreatValueAs = FormattedLinkLabel.TreatValueAs.FormattedText
  AddHandler lLink.LinkClicked, AddressOf OnLinkClicked

其中 Request.ID、Request.Description 以及 RndRes.Forms.txt_* 都是字符串。我想在 Infragistics FormattedLinkLabel 中显示我在此处构建的字符串。问题是,如果 Request 类中的任何字符串包含 HTML 字符,则标签将无法正确显示,并且 HTML 编码会被破坏。 我需要找到一个屏蔽 HTML 代码的函数。

I have the following code:

  Dim lStatementText As String
  Dim lStatementString As New System.Text.StringBuilder

  lStatementString.Append(RndRes.Forms.txt_request)
  lStatementString.Append(" ")
  lStatementString.Append("<b><a>")
  lStatementString.Append(Request.ID)
  lStatementString.Append("</a> - <a>")
  lStatementString.Append(Request.Description)
  lStatementString.Append("</a></b> ")
  lStatementString.Append(RndRes.Forms.txt_IsNotYetLoaded)
  lStatementString.Append(". ")
  lStatementString.Append(RndRes.Forms.txt_click)
  lStatementString.Append(" <b><a>")
  lStatementString.Append(RndRes.Forms.txt_here)
  lStatementString.Append(" </a></b> ")
  lStatementString.Append(RndRes.Forms.txt_GetFromDB)

  lStatementText = lStatementString.ToString()

  Dim lLink As New Infragistics.Win.FormattedLinkLabel.UltraFormattedLinkLabel()
  lLink.Value = lStatementText
  lLink.TreatValueAs = FormattedLinkLabel.TreatValueAs.FormattedText
  AddHandler lLink.LinkClicked, AddressOf OnLinkClicked

Where Request.ID, Request.Description and also RndRes.Forms.txt_* are all strings. I want to display the string I build up here in a Infragistics FormattedLinkLabel. The problem is that if any of the strigs from the Request class contains a HTML character, the label is not displayed properly and the HTML encoding is broken.
I need to find a function that masks the HTML codes.

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

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

发布评论

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

评论(2

迷你仙 2024-09-19 03:20:36

如果您手头没有 HttpContext 对象,请查看 HttpUtility.HtmlEncode:

http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

If you don't have an HttpContext object at hand, look at HttpUtility.HtmlEncode:

http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

山田美奈子 2024-09-19 03:20:36

尝试 Server.HtmlEncode() 和 Server.HtmlDecode(),http://msdn .microsoft.com/en-us/library/hwzhtkke.aspx

Try Server.HtmlEncode() and Server.HtmlDecode(), http://msdn.microsoft.com/en-us/library/hwzhtkke.aspx

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