How to Display as Html content in Textbox - asp.net

How to Display Html content in Textbox Control
Eg:
string text="<b>" + Hi How are you + "</b>";
txtEditor.Text=text.ToString();
I have to display those text in bold Letters, How do i achieve this

You can change the font property of the TextBox:
txtEditor.Font = new Font(txtEditor.Font, FontStyle.Bold);

See this link for an existing html editor. I strongly recommend that you carefully read the paragraph about Cross Site Scripting, whether you decide to use that control or not.

To display text bold in asp:textbox control, you can use style at design time or runtime
in .aspx page Design time
<asp:TextBox ID="textBox1" style="font-weight:bold;" runat="server"></asp:TextBox>
OR
in Code Behind
textBox1.Font.Bold = true;
You can try any one to get desired result. My suggestion over here is that use CSS style to get more advanced formatting in textbox

Related

Replace HTML with PlaceHolder control in ASP.NET

Is there any way to read some HTML and replace that HTML with a PlaceHolder control dynamically at runtime?
For example, I have some HTML that contains tags such as ##MainContent## or ##SideContent##.
I need to somehow find each tag and dynamically add a asp:PlaceHolder control.
EDIT - The reason for this is to read the HTML in and then dynamically add controls to a section of the page. I'm attempting to create a CMS system, although I'm not sure this is the best approach.
I hope this is a thought exercise. :)
Ideally, you should just replace those tags in your markup with an <asp:PlaceHolder> control. If you cannot do that (for whatever reason), try replacing them with an <asp:Literal> control:
<asp:Literal ID="litMainContent" runat="server" Text="##MainContent##"><asp:Literal>
<asp:Literal ID="litSideContent" runat="server" Text="##SideContent##"><asp:Literal>
That way, they will still render on the page exactly as they do now. Then you should be able to use litMainContent and litSideContent in your code-behind:
For VB.Net:
Dim phMainContent As PlaceHolder = New PlaceHolder
Dim phMainContent As PlaceHolder = New PlaceHolder
litMainContent.Controls.Add(phMainContent)
litSideContent.Controls.Add(phSideContent)
For C#.Net
PlaceHolder phMainContent = new PlaceHolder();
PlaceHolder phMainContent = new PlaceHolder();
litMainContent.Controls.Add(phMainContent);
litSideContent.Controls.Add(phSideContent);

How to set HtmlEditorExtender's content server-side

I'm using the AjaxControlToolkit's HtmlEditorExtender in my ASP.NET 4.0 web app:
<asp:TextBox ID="myTxt" runat="server" TextMode="MultiLine" Height="80px" Width="100%" />
<act:HtmlEditorExtender ID="heMyTxt" runat="server" TargetControlID="myTxt">
<Toolbar>
etc...
</Toolbar>
</act:HtmlEditorExtender>
When I set the content of the text box server-side like this:
myTxt.Text = htmlStringFromDatabase;
...the content in the textbox is the literal HTML markup (i.e. <b>Bold</b> shows up just like that, not like Bold). The formatting doesn't transfer, but the Extender does do its work on the textbox and set up its toolbar and buttons, etc. Is there a different way to set the content?
EDIT: turns out the HTML I get out of myTxt (the control that the extender is attached to) is encoded HTML. So now the question is how to stop the control from encoding its content. This problem is also presented in this question, but I'm not using LoadControl() or the designer to my page; I've written my markup manually.
Also, I don't know if this makes a difference, but I'm pulling the text out of the TextBox in the page's Page_Load handler.
Try to do like this,
myTxt.Text = HttpUtility.HtmlDecode(htmlStringFromDatabase);
I was able to solve this problem like this :
Literal lit = new Literal();
lit.Mode = LiteralMode.PassThrough;
lit.Text = HttpUtility.HtmlDecode(HTMLTExt);
TextBox1.Text = lit.Text; // The text box which HTMLEditorExtender is attached to

Display large text from database on asp.net page with proper spacing and indent?

I was just wondering out of curiosity if it was possible to display large text from a database in a HTML or asp.net page with proper indenting and spacing?
As of right now, I have it displayed in a textbox and I'm using a highlight specific text feature incorporated in my site that doesn't function ONLY when the length of the text is greater then actual textbox. So I was thinking of displaying the data on a page and considering using Response.Write but the displayed text is all bunched together without proper indenting or spacing.
Ex.) instead of..."Hey Everyone how is everyone doing today? I am sample text"...is possible to display it as...
"Hey Everyone
How is everyone today? I am sample text"
Would anyone guide to any proper method that I could use or know of any info
New Line will be like \r\n in code behind text
Now, you can use Span or Literal control like below in HTML
HTML Side
<span id="span" runat="server"></span>
<asp:Literal id="literal" runat="server"></asp:Literal>
Code Behind side
span.InnerHtml = yourtext.Replace("\r\n","<br/>")
Without knowing anything about what format the text is stored in the DB (I'm assuming it's just regular spacing & carriage returns.
You could put it between <pre> tags. e.g.
ASPX
<pre>
<asp:Label ID="myTextArea" runat="server" />
</pre>
CODE BEHIND
myTextArea.Text = //Code to get text string from DB
If there are carraige returns already embedded in the text, you could do replace them with <p> tags, like:
string value = // get value from database ;
string formattedValue = string.Format("<p>{0}</p>", value.Replace("\r", "</p><p>"));

ASP.NET: checkboxlist with textbox?

I'm working in ASP.NET and I have a CheckBoxList where I want one of the options to be basically like "Other: _." So I need to include a textbox where the user can fill in their own option. It doesn't seem like there's a way to include a textbox inside of a checkboxlist, however. What's the best way to make this work?
-UPDATE-
If using a separate textbox control, how do I position it so it will align correctly with the checkbox?
Make the textbox a separate control on the page, then in your codebehind, check to see if other is checked. If it is, pull the value of the textbox, and use that.
To answer the question in your edit: You'll have to play with the CSS of the page to get it positioned correctly. How you do it depends on the layout of the page, among other things. I recommend posting some of the HTML from your page in another question and ask about how to position them.
What #Kyle Trauberman said...
Make the textbox a separate control on
the page, then in your codebehind,
check to see if other is checked. If
it is, pull the value of the textbox,
and use that.
Plus use javascript to hide or gray out the option unless the checkbox is selected.
string test="";
<asp:CheckBoxList ID="chk_list" runat="server">
<asp:ListItem Value="00">xxxx</asp:ListItem>
</asp:CheckBoxList>
<asp:TextBox ID="other" runat="server"></asp:TextBox>
inside the for loop
if (chk_list.Items[i].Value == "00")
{
test +=chk_list.Items[i].Text + other.Text;
}

What's the Literal control used for and what's the difference to the Label Control in asp.net?

What Literal control is used for in asp.net? and What is the difference between them and Label control?
The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:
<span>My Label text</span>
The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.
Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.
P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)
For example:
<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />
Will render as:
<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />
See also here on W3 Schools.
One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.
I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.
It is used to display text on the page, the text that is displayed can be set at runtime via server side code.
The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.
Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.
MSDN on AssoicatedControlId
As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.
It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.
We can use literal control in the title tag whereas label cannot be used in the title tag
Label can be used to set focus on other controls like Textbox.
Whereas Literal simply rander the static text on the web page

Resources