TextArea Save/Retrieve from the Database - c#

I am trying to save the data in a TextArea into a column in my database.
Then retrieve the data i saved from the database into a label or div.
Using this in HTMl to save:
<textarea runat="server" id="TextArea1" cols="100" rows="10" ></textarea>
Using this in c# to save:
cmd2.Parameters.AddWithValue("#s_qualification", TextArea1.InnerText);
I dont have any problem saving.
But when i retrieve the data from the database, the data does not have any linebreaks.
test2.InnerText = myReader3["s_qualification"].ToString();
Edit: When i display the data in a text area, it displays fine but its not the way i want it to.. The text needs to displayed like a label as its for a profile page's description
ps: using visual studio 2010, c#
Sorry for the trouble, Everything fixed:
test2.InnerHtml = myReader3["s_qualification"].ToString().Replace(Environment.NewLine, "<br />");

I entered the following text into the text box:
This is a test of the line breaks to see if a text area actually include line breaks \n
or is is just a magic word wrapping that happens \n
and it needs to be done manually.
Where \n shows where I pressed the Enter key.
var myText = TextArea1.Text;
string newText = myText.Replace("\r\n", "<br/>");
Results in newText:
This is a test of the line breaks to see if a text area actually include line breaks <br/>or is is just a magic word wrapping that happens <br/>and it needs to be done manually.
So,
test2.InnerText = myReader3["s_qualification"].ToString().Replace("\r\n", "<br/>");
Should do it.

Probably you can not detect line breaks at while you visualize on
test2.InnerText = myReader3["s_qualification"].ToString();
instead you sholud try to visualize on
<textarea>your text</textarea>
Note: Jon \n\r Skeet will be displayed in one line on visual studio watch window but two lines on textarea

Much better approach (if you display text in a label) is this: assign this CSS to your label:
label{
white-space: pre-wrap;
}
Source:
www.w3schools.com/cssref/pr_text_white-space.asp

Related

ASP.NET HTML Encoded Data-Bind and br tag

I'm trying to encode data that is to be displayed on a separate page (.aspx) after a user submits a form with their information and questions. There are both text input and textarea fields that the user can fill out.
I'm using the <%#: %> expression to place this data on the page since it does the HTML encoding, but have run into an issue with the encoding also encoding the br tags as text instead of actual line breaks.
Is there a way to make it so that the br tags still cause a line break, but leave everything else encoded using this method?
Example of user input:
Hi
This is what should be responded with.
Here is an example of the code that places it on the page:
<p><%#: ((DiscussionThread.Discussion)Container.DataItem).Text %></p>
What it currently displays on the page:
Hi<br /><br />This is what should be responded with.
Thanks in advanced!
Don't know, will it help you in your case or no, but I always use next way:
string forInput = yourTextBoxID.Text.ToString().Replace(Environment.NewLine, "%NL%");
string forOutput = forInput.Replace("%NL%", Environment.NewLine);
If in forOutput Environment.NewLine will not work, change it to <br />
%NL% it is just a identificator, u may change to anything else.

How to make sure that my <br> tag get read as a html and not as text

I have a asp.net page. On one particular page i can add comments and when i add these comments they get saved to a db. When i want new lines the "\n" tagg is automaticly displayed when i look at it in my backend code.
So now when i get back a response i want to get my comments back but i want them to have breaking spaces on those particular places where the "\n" is displayed.
I tried this in my backend code(cs):
Text = text.replace("\n", "<br/>")
the result i get:
my test<br/> test text new line<br/>
My <br> tags get displayed as text.
To clarify i basicly wan to achieve this:
Why are my "<br />" tags getting converted to "<br />"?
But from the backend code in C#.
Ive been googling and haven't been able to find a answer to this.
If you are using ASP.NET WebForms and you are putting the text in a Label control, this is normal behaviour. The Label control html-encodes its text before rendering it.
The Literal control does not use html-encoding, so if you change your Label to a Literal, it should work.
If you are using ASP.NET MVC, try using #Html.Raw(Model.Text).
You need to create an HtmlString with your content before printing it. So your code would be like
var foo = new HtmlString(text.replace("\n", "<br/>"));
and you would print foo.

How to store multiline text in SQL Server 2008 keeping track of new line in asp.net

I am developing an intranet ASP.NET web site. I am saving user comments from an asp.net multiline textbox into a SQL Server database, and display exactly as it is written by user in Gridview.
Suppose a user enter following string:
1.Task1
2.Task2
3.Task3
then the retreived data in the gridview should be same as user entered it before saving.
While debugging, I tried replacing \r\n with <br/> but gridview is displaying <br/> as it is..not tasking a new line.
I am using c#.
I have found the solution for it Using Literal Control.
Now, there is one new Problem where i am not using Gridview.I am generating Comments as html and sending it as a mail body,
Suppose a user enter following string:
1.Task1
a.T1
b.T2
c.T3
2.Modules
a.Module A
b.Module B
c.Module C
Line Break <br /> when Replaced with /n is taking the content only to new line not taking spaces for Second Line Automatically.and the mail body html is generating it like this:
1.Task1
a.T1
b.T2
c.T3
2.Modules
a.Module A
b.Module B
c.Module C
Which is not what i Expect.
Please Help. Thanks.
You can use carriage returns when you save the text and when displaying it in the grid, you can replace it as
'<%# Eval("TheName").ToString().Replace("\n", "<br />") %>'

How to enter the text in textarea using mshtml

I am facing a problem I have to fill a TextArea programatically. I used the following code.
System.Windows.Forms.HtmlElement reviewText = myDoc.GetElementById("review-text");
reviewText.InnerText = Program.mainForm.richTextBox1.Text.Trim();
This works fine and it sets the text in TextArea control. The problem that I am facing is that this text appears light gary. When the user clicks over this text it disappears. It happens only on first click.
So I tried the following code to first click this box and then set the text.
reviewText.InvokeMember("click");
And then tried to set the text in the TextArea but got the same behaviour.
I dug into the source of the page and found some script associated with this TextArea.
Source of TextArea
<textarea onkeyup="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" onkeydown="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" name="comment" id="review-text" class="form400" rows="8" cols="40" style="height: 86px;"></textarea>
Script associated with TextArea
<script type="text/javascript">
var bizName = "Geary Club";
if ($('review-text'))
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
Will somebody suggest to me how to insert the text into this TextArea control?
Is this an ASP.NET web app? If it is, the System.Windows.Forms.HtmlElement is a problem. What I think is going on is that you need to somehow disable the new yelp.ui.widget.DefaultValueTextField JavaScript code when you're pre-filling the TextArea. Are you able to use an ASP.NET TextBox control with the multi-line properties set? If so, I'd use that and change your top code to just set the .Text property of the Textbox control.
Either way, in the Javascript (if this is jQuery), I'd do this:
if ($('review-text') && $('review-text').val() != "")
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
That way the default value is only set if there is no text.
I changed the HTML of text area.And remove the class="form400 default_valued_text_field" attribute from its html.
myDoc = this.webBrowser1.Document;
myDoc.GetElementById("review-text").OuterHtml = "<textarea id=\"review-text\" onkeyup=\"javascript:ui.widget.countChars(this.form.comment,5000);\" onkeydown=\"javascript:ui.widget.countChars(this.form.comment,5000);\" name=\"comment\" rows=\"8\" cols=\"40\" style=\"height: 86px;\"></textarea>";
After that i set the inner text of this element
myDoc.GetElementById("review-text").InnerText = "Testing";

Using <br/> in tool tip and change the default color of a tool tip

I am using default asp.net ToolTip property, the text is
This is Going to be a long text
Thats why we decided to split it.
but using <br/> to split the line doesn't work, it renders as a text, and I want it to break the line instead.
Here is my code:
Label lblActionText = new Label();
lblActionText.Text = "Helloooo Phaltu";
lblActionText.Style.Add("cursor", "pointer");
lblActionText.ToolTip =
"This is Going to be a long text"
+ "<br/>"
+ "Thats why we decided to split it.";
I've not tried this but would "System.Environment.NewLine" not do the job instead of the BR tag?
You can use this to insert a line break inside a ToolTip.
lblActionText.ToolTip = " First text " + Environment.NewLine + " second text ";
Use the line break character entity (
). It is easy to implement but the only problem is that this will work in IE & Chrome but not Firefox.
lblActionText.ToolTip = "This is Going to be a long text
Thats why we decided to split it.";
See jsFiddle: http://jsfiddle.net/jafLf/
You are passing a string value to a Tooltip, and that's why the HTML element <br /> is not working.
However you can try this ASP.Net AJAX TooltipExtender
This works only on IE but other browsers are not!!
If you want to have a customize tooltip then seach for jQuery tooltips to have a formatted tooltip
You can do this with adding a table in the tooltip :)
Example: ASP forum
The ASP.NET ToolTip property corresponds to the HTML title attribute.
In IE and Chrome you can simply do:
<span title="multiline
tool
tip">Mouseover me!</span>
However, that won't work in Firefox as it actually follows the W3C guidelines for CDATA.
CDATA is a sequence of characters from
the document character set and may
include character entities. User
agents should interpret attribute
values as follows:
Replace character entities with characters,
Ignore line feeds,
Replace each carriage return or tab with a single space.
Your best bet (considering you also want to change the colour) would be to go with a jQuery solution such as QTip which gives you all the customisation you want and more..
You cannot change the color of a tooltip with the default title attribute. For that the only way is to use a javascript generated tooltip.
There are plenty of plugins for jQuery such as:
Dynamic tooltip
Popup Bubble
jQuery Horizontal Tooltips
Coda Popup Bubble
Awesomeness
TipTip
(mb)Tooltip
vTip
jGrowl
jQuery Ajax Tooltip
Digg-style post sharing tool with jQuery
Input Floating Hint Box
Simpletip
qTip
Orbital Tooltip
And many more, check them here: Stylish jQuery Tooltip Plugins Webdesign
You can also just create your own using javascript. Add a mouseover event on the element then show a hidden div over the element with whichever html elements you want, in whichever color you need.
It is also more safe to use a javascript approach for cross-browser compatibility.

Categories