1

如何将 HTML 格式粘贴到 C# 中的字符串对象中?

这是我所拥有的:

c.DepartmentAbbr.ToString() + " - (" + c.DepartmentName.ToString() + ")"

其中 c.DepartmentAbbr.ToString() 和 c.DepartmentName.ToString() 都是使用 LINQ 从数据上下文中选择的字段。

这就是我想要的:

"<b>" + c.DepartmentAbbr.ToString() + "</b> - (" + c.DepartmentName.ToString() + ")"

以便第一个单词以粗体显示。上面只显示了带有粗体标签的文字文本和所有内容。我假设我需要使用 String.Format 但我找不到一个很好的例子来帮助我知道如何使用它来做我想做的事。

更新

这里还有一些我认为不重要的细节,但我认为现在它们一定很重要。

这是我正在使用的控件。ASP 代码:

 <telerik:RadComboBox ID="rcbDepartments" runat="server" AppendDataBoundItems="True"
        AutoPostBack="true" NoWrap="true" Width="250px">
        <Items>
            <telerik:RadComboBoxItem Text="All Departments" Value="-1" />
        </Items>
    </telerik:RadComboBox>

我正在使用 C# 中的 LINQ 向此控件添加项目:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var abbr = from c in DB.Departments
                   where c.DepartmentAbbr != "BInst"
                   select c;

        foreach (var c in abbr)
        {
            String s = String.Format("{0} - ({1})", c.DepartmentAbbr, c.DepartmentName);
            rcbDepartments.Items.Add(new RadComboBoxItem(s, c.DepartmentID.ToString()));
        }
    }
}

RadComboBoxItem 对象接受 ()、(String text) 或 (String text, String value),我使用的是后者。

4

7 回答 7

9
String.Format("<b>{0}</b> - ({1})", c.DepartmentAbbr, c.DepartmentName)

这是 C# 中字符串格式化的一个很好的参考:

http://blog.stevex.net/string-formatting-in-csharp/

于 2009-12-22T23:49:27.543 回答
4

对于任何试图在ASP.NET MVC中执行此操作的人...

这不起作用:

<%=String.Format(resourceString, "<b>" & Model.UserName & "</b>")%>

如您所料,粗体标签是 HTML 编码的。

但是,这是一种享受:

<%=String.Format(resourceString, MvcHtmlString.Create("<b>" & Model.UserName & "</b>")%>
于 2012-04-25T11:48:33.890 回答
2
MvcHtmlString s = MvcHtmlString.Create(String.Format("{0} - ({1})", c.DepartmentAbbr, c.DepartmentName));

我认为您最好将 String 更改为 MvcHtmlString 对象。

于 2015-04-22T09:37:24.543 回答
1

我猜您正在寻找一种方法让对象c吐出 HTML 格式的字符串。

以此为例:

公共类 myExample : IFormattable{
   私有字符串 myExampleStr;
   公共 myExample(字符串 sampleStr){
      this.myExampleStr = sampleStr;
   }

   /* 实现一个 Equals() 函数 - 覆盖!*/
   公共覆盖布尔等于(对象obj){
      返回真;
   }

   /* 实现一个 ToString() 函数 - OVERRIDE */
   公共覆盖字符串 ToS​​tring(){
      返回 this.myExampleStr;
   }

   /* 实现一个 GetHashCode() 函数 - OVERRIDE */
   公共覆盖 int GetHashCode(){
      返回 this.myExampleStr.GetHashCode();
   }

   /* 这里我们实现了 IFormattable 接口 */
   公共字符串 ToS​​tring(字符串格式){
      返回 this.ToString(format, null);
   }

   公共字符串 ToS​​tring(IFormatProvider formatProvider) {
      返回 this.ToString(null, formatProvider);
   }

   公共字符串 ToS​​tring(字符串格式,IFormatProvider 格式提供者){
      if (string.IsNullOrEmpty(format)) 格式 = "G";
      如果(格式提供者!= null){
         ICustomFormatter 格式化程序 = formatProvider.GetFormat(this.GetType()) 作为 ICustomFormatter;
         如果(格式化程序!= null)
            返回 formatter.Format(format, this, formatProvider);
      }
      开关(格式){
         case "b": return string.Format("<b>{0}</b>", this.myExampleStr);
         case "i": return string.Format("<i>{0}</i>", this.myExampleStr);
         默认值:返回 this.myExampleStr;
      }
   }
}

假设我们像这样实例化这个类:

myExample 示例 = new myExample("tommieb75");

并在对象上发出一个简单ToString()的调用以打印出粗体的 HTML 标记,如下所示

Console.WriteLine("{0}", example.ToString("b"));
// 输出将是 <b>tommieb75</b>

此示例用于说明如何对ToString()方法使用自定义参数,以便您可以使用嵌入的粗体或斜体 HTML 标记,或基于用于ToString此类函数的参数格式化值的任何其他方式。

这回答了你的问题了吗?

于 2009-12-23T00:44:03.543 回答
0

I think the problem is somewhere else. Are you putting this string in a Label control or a Literal control? If it's a Label, I believe it's escaping the text for you. Put it in an asp:Literal control instead. And btw, you don't need to call .ToString() so much. You can just use:

"<b>" + c.DepartmentAbbr + "</b> - (" + c.DepartmentName + ")"

and ToString() will essentially be called for you.

于 2009-12-22T23:50:32.037 回答
0

随着我研究得越来越多,我几乎得出结论,我想做的事情是不可能的。我正在使用 RadComboBox 将项目粘贴到其中,但我必须在服务器端使用 LINQ 做一些古怪的事情,这样我必须使用 Items.Add 方法将项目粘贴到组合框中只接受系统。细绳。谢谢大家的帮助和评论,但我想我只需要处理我在这种情况下所拥有的一切。

这里还有一些我认为不重要的细节,但我认为现在它们一定很重要。

这是我正在使用的控件。ASP 代码:

我正在使用 C# 中的 LINQ 向此控件添加项目: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var abbr = from c in DB.Departments where c.DepartmentAbbr != "BInst" select C;

        foreach (var c in abbr)
        {
            String s = String.Format("{0} - ({1})", c.DepartmentAbbr, c.DepartmentName);
            rcbDepartments.Items.Add(new RadComboBoxItem(s, c.DepartmentID.ToString()));
        }
    }
}

RadComboBoxItem 对象接受 ()、(String text) 或 (String text, String value),我使用的是后者。我现在很确定,由于 RadComboBoxItem 的限制只接受字符串,我不能在不使用 ASPX 中的项目模板标签的情况下对项目进行 HTML 格式设置。不幸的是,对于我的情况,我不能这样做,因为这已经给我带来了一些其他问题,这就是我为什么要做我目前正在尝试做的事情的原因。

于 2009-12-23T16:07:05.770 回答
-1

如果您在网页上输出此内容 - 请尝试使用:

Server.HtmlEncode(string)
Server.HtmlDecode(string)

方法。

于 2009-12-22T23:47:00.917 回答