How do I stick HTML formatting into a String object in C#?
Here’s what I have:
c.DepartmentAbbr.ToString() + " - (" + c.DepartmentName.ToString() + ")"
where c.DepartmentAbbr.ToString() and c.DepartmentName.ToString() are both fields being selected from a data context using LINQ.
Here’s what I essentially want:
"<b>" + c.DepartmentAbbr.ToString() + "</b> - (" + c.DepartmentName.ToString() + ")"
so that the first word shows up in bold. The above just shows the literal text with the bold tags and everything. I assume I will need to use String.Format but I can’t quite find a good example that helps me know how to use it to do what I want.
Update
Here are a few more details that I didn’t think were important but I think by now they must be.
Here is the control I’m using. ASPX code:
<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>
And I’m adding items to this control using LINQ in C#:
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()));
}
}
}
The RadComboBoxItem object accepts either (), (String text), or (String text, String value), and I am using the latter.
As I’ve researched more and more I’ve pretty much come to the conclusion that what I want to do is not possible. I’m using a RadComboBox to stick the items into, but I’m having to do some funcky stuff with LINQ on the server side such that I have to stick the items into the combo box using the Items.Add method which only accepts System.String. Thank you all for your help and comments but I think I’m just going to have to make do with what I have in this case.
Here are a few more details that I didn’t think were important but I think by now they must be.
Here is the control I’m using.
ASPX code:
And I’m adding items to this control using LINQ in C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var abbr = from c in DB.Departments
where c.DepartmentAbbr != “BInst”
select c;
The RadComboBoxItem object accepts either (), (String text), or (String text, String value), and I am using the latter. I am pretty sure by now that due to the restrictions on the RadComboBoxItem only accepting Strings that I can not do HTML formatting on the items without using the Item Template tags in ASPX. Unfortunately for my situation I can not do this because that was already causing some other problems for me which I why I’m doing what I am currently trying to do.