How to catch "Enter" command a in Textarea element in server-side? - asp.net

I'm building a app that I need to catch the text of textarea with all lines separated. That is, if the user typed in textarea and press "Enter" to do a new line. Is there a way I can get the text from textarea and know the places where the user lines separated by "Enter"?
From my tests, in Internet Explorer I can get using Environment.NewLine:
string someString = TextArea1.Text.Replace(Environment.NewLine, "<br />")
But using Firefox or Chrome did not work.

try this:
string someString = TextArea1.Text.Replace( Convert.ToString(Convert.ToChar(13)) + Convert.ToString(Convert.ToChar(10)), "<br />")
edit
thats the final solution after trying some others.
please look at history of this question for more solutions. :)

I ran my own test, and this worked for me.
TextBox1.Text = TextBox1.Text.Replace("\r\n","<br />");
Something else is wrong if it is not for you.

I have also had problems with "\r\n" in the past. However, this has always seemed to work for me:
string someString = TextArea1.Text.Replace("\n", "<br />");
I also like to be anal about making sure nothing gets missed so I often do:
string someString = TextArea1.Text.Replace("\r\n", "<br />").Replace("\n", "<br />");
But the first one should work fine.
Good Luck!

Related

VB.NET 2.0: Where does a URL in code come from?

I have to debug an old VB.NET 2.0 code of someone who has left the company. We have a production system (let us call it http://prod) and a test system (http://test). Both are nearly similiar including a documents repository. When looking at docs in production, all the hyperlinks showing up at the bottom are okay (meaning they say something like http://prod/download.ashx?id={GUID}).
However in test it is the same (http://prod/download.ashx?id={GUID}), even it should be http://test/download.ashx?id={GUID} instead.
After hours of debugging I have found the relevant line of code:
html += "<td><a href='" + HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/") + "int/download.ashx?id=" + row.Item(0).ToString() + "' target='_blank' class='" + row.Item(3).ToString() + "'>" + row.Item(1).ToString() + "</a>" + privat + "</td><td>" + row.Item(2).ToString() + "</td>"
Looking at html this shows i.e.
"<table class='table_dataTable'><thead><tr><td>Name</td><td>Jahr</td></tr></thead><tbody><tr><td><a href='http://prod/int/download.ashx?id=4d280886-db88-4b25-98d8-cf95a685d4a4' target='_blank' class='doc'>Document for managers</a></td><td>2014</td>"
So I wonder, where does this come from incorrectly? I may have found the relevant part of coding, but I am not sure, what to do now, respectively if I am right on this?:
Public Class download : Implements IHttpHandler, IReadOnlySessionState
Dim debug As String = ""
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fehler As String = ""
Try
' Get the file name from the query string
Dim queryFile As String = context.Request.QueryString("id")
debug += "id=" + queryFile + "<br>"
Any help is appreciated as VB.NET is not my main focus.
You have probably checked this but sometimes the obvious gets overlooked.
Verify the URL in your browser window. Make sure it has not changed to http://prod... while you were navegating.
Verify that your web application is not using frames. The page in question could be loaded in a frame using the prod URL. If this is the case your web.config might have a setting to say where this frame is loaded from or it might simply be hardcoded.
Check for URL Rewrite rules in IIS or your web.config

If RadioButton.Checked

I need help loading text file in TextBox3.Text with 3 possible options.
1. If there is nothing in TextBox.Text
2. If there is some help text explication to say user what he have to load
3. if correct path then..
I have all good if I choose only from pathn but I need the 3 option.
My code:
If (TextBox3.Text = "Enter the path from your computer to your text file") And (TextBox3.Text = "") And RadioButton1.Checked = True Then
Dim FILE_NAME = Path.Combine(Directory.GetCurrentDirectory(), "\default.txt")
End If
If TextBox3.Text <> "" Then
FILE_NAME = TextBox3.Text
End if
PS: Dim file_name is already declared, but i didnt want to enter here all that codes. Also I tried to not add () and laso looked in MSDN declaration and did exactly same but it wont load nothing, if I dont enter path, OR if I let the explication to user so it wont load default.txt
Thank you
I would like to explain to you what I have been (trying) to explain in the comments (obvoiusly) without much success:
You have written, as your first line:
If (TextBox3.Text = "Enter the path from your computer to your text file") And (TextBox3.Text = "") And RadioButton1.Checked = True
So, YOU are testing (in a shorter way):
IS(TextBox got the value of "Enter the path from your computer to your text file")?
Yes?
Then IS (textBox ALSO the value of "")
Yes?
Then IS (the radio button also checked?)
However, you're code will NEVER REACH the 'is the radiobutton...' part, Since textbox cannot be both
"Enter the path from your computer to your text file" AND "" (nothing/Blank)
I hope you now understand the reason for your code never executing that part of your first if statement.
It's impossible for the code to be true with that condition, and I hope now you will be able to see why.

Message box doesnt work after changing text?

I wanted to change the text of the message box from "Are you sure ...?" to "You're information is ready to be submitted" When I did this using the code below the message box no longer works. Does this code rely on the "Are you sure" portion?
Try
Dim msg As String = "Hello!"
Dim script As String = "if(confirm('You're information is ready to be submitted')) {window.location.href ='frmMain.aspx'; }"
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "Test", script, True)
Catch ex As Exception
End Try
The apostrophe in " You're " needs to be escaped. Replace " You're " with " You\'re ".
Edit: A good point was brought up in the comments - grammatically, it should be " Your ".

String.Replace not replacing vbCrlf

I am trying to replace all the occurrences of "\n" in the Text property of an ASP.NET TextBox with <br /> using String.Repalce function, but it doesn't seem to work:
taEmailText.Text.Replace("\n", "<br />")
As a solution I am using Regex.Replace:
New Regex("\n").Replace(taEmailText.Text, "<br />")
My question is why String.Replace can't find "\n" for me, even though this solution has been proposed on many sites and it has worked for many people.
Thanks,
In .NET string objects are immutable, so String.Replace returns a new string with the replacement. You need to assign the result:
taEmailText.Text = taEmailText.Text.Replace("\n", "<br />")
Also, rather than creating a new Regex object—when you do need a regular expression—then there are static methods available:
result = Regex.Replace(input, pattern, replacement)
Edit (based on comment):
Just tested this:
Sub Main()
Dim result As String = "One\nTwo".Replace("\n", "<br />")
Console.WriteLine(result)
End Sub
and the result is:
One<br />Two
The problem is the result of the method call is immediated forgotten. You should read MSDN documentation a bit more carefully:
Returns a new string in which all occurrences…
Hence do:
taEmailText.Text = taEmailText.Text.Replace("\n", "<br />")
Replace does not change the content of the input string. It returns newly created string.
You might want to replace both \r and \n or use Environment.NewLine constant.
var replacedText = TextBox1.Text.Replace(Environment.NewLine, "<br />");

New to asp.net. Need help debugging this email form

First of all, I am a php developer and most of .net is alien to me which is why I am posting here!
I just migrated over a site from one set of webhosting to another. The whole site is written in .net. None of the site is database driven so most of it works, except for the contact form. The output on the site simple states there was an error with "There has been an error - please try to submit the contact form again, if you continue to experience problems, please notify our webmaster." This is just a simple message it pops out of it gets to the "catch" part of the email function.
I went into web.config and changed the parameters:
<emailaddresses>
<add name="System" value="roeland#hoyespharmacy.com"/>
<add name="Contact" value="roeland#bythepixel.com"/>
<add name="Info" value="roeland#bythepixel.com"/>
</emailaddresses>
<general>
<add name="WebSiteDomain" value="hoyespharmacy.com"/>
</general>
Then the .cs file for contact contains the mail function EmailFormData():
private void EmailFormData()
{
try
{
StringBuilder body = new StringBuilder();
body.Append("Name" + ": " + txtName.Text + "\n\r");
body.Append("Phone" + ": " + txtPhone.Text + "\n\r");
body.Append("Email" + ": " + txtEmail.Text + "\n\r");
body.Append("Fax" + ": " + txtEmail.Text + "\n\r");
body.Append("Subject" + ": " + ddlSubject.SelectedValue + "\n\r");
body.Append("Message" + ": " + txtMessage.Text);
MailMessage mail = new MailMessage();
mail.IsBodyHtml = false;
mail.To.Add(new MailAddress(Settings.GetEmailAddress("System")));
mail.Subject = "Contact Us Form Submission";
mail.From = new MailAddress(Settings.GetEmailAddress("System"), Settings.WebSiteDomain);
mail.Body = body.ToString();
SmtpClient smtpcl = new SmtpClient();
smtpcl.Send(mail);
}
catch
{
Utilities.RedirectPermanently(Request.Url.AbsolutePath + "?messageSent=false");
}
}
How do I see what the actual error is. I figure I can do something with the "catch" part of the function.. Any pointers?
Thanks!
Change the catch to
catch(Exception ex)
{
throw;
}
The ex variable will hold your exception information, so you can put a breakpoint there. It'd be easier to step through it, but you can just throw the error as well.
Comment out Utilities.RedirectPermanently(Request.Url.AbsolutePath + "?messageSent=false");
and replace it with throw;
What development environment are you using?
It's probably best to use Visual Studio (Express if you don't have the full version), and debug this code, hitting F11 to step through each statement until it breaks. Then you should have access to more information.
I would suggest logging in the long term (such as log4net). But for the sake of speed try changing your catch statement to look like:
catch(Exception e)
and then use the debugger in VS to explore the actual exception.
Place a breakpoint on the first line of the EmailFormData method and run the application in debug mode. You can then step through the code line by line.

Resources