How to make string bold in code behind - c#

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.

You can't make some bits bold and some bits not bold, in an <asp:TextBox>.

You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;

PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.

A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "𝗧𝘆𝗽𝗲 𝗢𝗳 𝗗𝗮𝘆: 𝖣𝖾𝗅𝗂𝗏𝖾𝗋𝗒 𝖣𝖺𝗒"

You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.

TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

Related

How do i remove "System.Windows.Forms.TextBox" from the output text?

text2 = string.Format("[B](No Change)[/B]");
The output comes out as:
System.Windows.Forms.TextBox, Text: (No Change)
How can I remove the System.Windows.Forms.TextBox, Text: from my output?
Well a wild guess, but I think what you are trying to print is not the Text property of the textbox, instead the whole textbox.
Suppose you have:
TextBox1.Text = string.Format("[B](No Change)[/B]");;
Console.Write(TextBox1);
Then you will get the output.
System.Windows.Forms.TextBox, Text: [B](No Change)[/B]
What you probably need to do is to use Text property.
Console.Write(TextBox1.Text);
This will give you the assigned text and will exclude. System.Windows.Forms.TextBox, Text: part
I'm making a guess here but are you trying to make "No change" appear as bold text? In that case, you can modify the textbox font itself rather than try and format your string.
text2.text = "No change";
text2.Font = new Font(text2.Font, FontStyle.Bold);
If that's not what you were trying to do then sorry!
Do not forget to add Text
text2.Text = string.Format("[B](No Change)[/B]");

Adding HTML property in C# when dealing with email

I am facing a situation where I need to process an incoming email which is in plain text format and then display the email as virtual plain text(Font Size =10.5 and Font type=Consolas) to the user.
My code till now
string Text = "<html><body><basefont size=3 font face= consolas>" + mailItem.HTMLBody + "</font></body></html>";
mailItem.HTMLBody = Text;
//Process the email and display
This works good, but during display this shows virtual plain text of font size 10.0 instead of needed 10.5. I tried changing the basefont size to 4,5 etc. this does not change the display in any way.
other options tried:
1.Adding a CSS stylesheet. This will not work good as most email clients don't support it.
2.Adding a div tag to the code like this
string Text = "<html><div style=font-size:10.5px; font-face:consolas;>" + mailItem.HTMLBody + "</font></body></div></html>"
this also does not work.
My main objective is to display a virtual plain text email with Font Size 10.5 and font type Consolas to the user after the processing has been done.
Your second approach seems to be allright - you just have a </font> and a </body> too much + you should change "font-face" to "font-family".
Also throw in double quotes - that will output one quote:
string Text = "<html><div style=""font-size:10.5px; font-family:Consolas;"">" + mailItem.HTMLBody + "</div></html>"

show line breaks in output

when i enter text into a text area with linebreaks this is not being output later when i display the text. how do i let it show the line spaces?, instead all the text is cramped together at the moment.
How do you display it? HTML ignores whitespace, including line breaks. You can replace the linebreaks with the <br> element to show the enters in your browser too.
Mind that any subsequent spaces are also ignored. You can use the <pre> element too, to display the exact text, but I think that long lines won't be wrapped in that case.
If you are putting your text into HTML try this just before writing it out to the page:
string myText = "" // your text
myText = myText.Replace(Environment.NewLine, "<br/>");
this worked:
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

Display url in text box

I have a textbox as follows:
<asp:TextBox runat="server" ID="txtOtherApps" Height="400" Width="400"
TextMode="MultiLine" ontextchanged="txtOtherApps_TextChanged" ></asp:TextBox>
How to display link in this textbox?
The TextBox allows you to display text which the user can edit. It does not allow you to display anything but plain text. To display a URL in the TextBox, simply set its Text property:
txtOtherApps.Text = "http://www.example.com/";
It wont, however, be a "link". Clicking the URL will cause a text cursor to be placed, allowing for the user to edit the URL.
It is possible if you use JavaScript
Use JavaScript on your text element - such that:
<input type="text" name="t1" id="t1" value="http://www.google.com" onmouseover="this.style.cursor='pointer' ;" onClick="window.open(this.value);"/>
Only Java script can do what you are asking for.
You won't be able to click on the link, but you can just set the Text property of the TextBox to the URL.
ASP.NET will render TextBoxes as textareas (in your case, because it's multiline) or inputs. These are standard HTML fragments which are just plain text containers. You can style them, but you can't really linkify the contents of them.
If you really just want to put the text of a link into a box, do this:
// either from the server:
txtOtherApps.Text = YourLinkString;
// or from the client:
<script>
document.getElementById('<%=txtOtherApps.ClientID%>').value = YourJsLinkValue;
</script>
If you want something to happen with the user clicks on the text area, you can add an onclick handler to it...but this would be weird.
You will need a RichTextBox. The .NET one is not available for web applications, but there are a few third party solutions available.
http://www.richtextbox.com/ is one of them, you will have to see for yourself if there is any available that suits your needs better.

When to use Literal vs LiteralControl?

What are the appropriate use of these two controls? From time to time I build up HTML in the code behind. Sometimes I want to output white space and I end up doing something like this.
const string twoSpaces = " ";
p.Controls.Add(new Literal { Text = twoSpaces });
or
const string twoSpaces = " ";
p.Controls.Add(new LiteralControl { Text = twoSpaces });
My question is, is this an appropriate use of these controls? Should I be adding whitespace this way? When do I use one over the other?
I realize I could probably do something with CSS, but I really want to know what are the purposes of these two controls, and is there anything inherently wrong with using them in this fashion.
Literal uses ViewState and will remember any updates to its properties across Postbacks. LiteralControl must have its properties set on every postback.
Other than that, be very careful when using Literal. If you allow user input to be rendered in a Literal tag, you have very likely opened up a XSS attack.
Chris Pitman has answered it correctly, but I just want to add some more facts about both.
Literal is a control which can be added on both an aspx page as well as to the code behind.
asp:Literal Text="text" runat="server"
But LiteralControl cannot be added on aspx page, but only to the code behind. So that is why LiteralControl doesn't have its ViewState associated to the page class.

Categories

Resources