Message box doesnt work after changing text? - asp.net

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 ".

Related

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.

Add a session variable to a script string?

I am using the code below to generate a popup box. Where is says "The information is ready to be submitted" I would like to add "Your reference number is 12345" I am getting that reference number using a session i.e. Session("ID"). Is there a way I can add this to the string?
Try
Dim msg As String = "Hello!"
Dim script As String = "if(confirm('The 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
Yep. Just add the information to your script string (I switched the string to stringbuilder for slight efficiency gain):
Dim sbScript As New System.Text.StringBuilder(200)
sbScript.Append("if(confirm('The information is ready to be submitted. Your reference number is ").Append(Session("ID"))
sbScript.Append("')) {window.location.href ='frmMain.aspx'; }")
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "Test", sbScript.ToString(), True)
Try this:
Try
Dim msg As String = "Hello!"
Dim idValue As String = CType(Session("ID"), String)
Dim script As String = "if(confirm('The information is ready to be submitted. Your reference number is " & idValue & "')) {window.location.href ='frmMain.aspx'; }"
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "Test", script, True)
Catch ex As Exception
End Try

ASP If Statement if Split() is empty as I get Subscript out of range: '[number: 1]'

I have a single form field that asks for "Full Name" when the user signs up. I want to then split that string into first name and then surname. I can get that working with the following code:
<%
If Request.Form("user[username]") <> "" Then
NameStr = Request.Form("user[username]")
Dim WordArray
WordArray = Split(NameStr, " ")
End If
%>
From that I can then split the variables into my form with:
value="<%=WordArray(0)%>"
value="<%=WordArray(1)%>"
However, if the user just puts their first name in I get an error as the Split is looking for a space between the words to perform the action correctly. I have tried:
<%
If Request.Form("user[username]") <> "" Then
NameStr = Request.Form("user[username]")
Dim WordArray
If NameStr = "" Then
WordArray = Split(NameStr, "")
Else
WordArray = Split(NameStr, " ")
End If
End If
%>
Now I knew this would fail as it will never be blank but is there a way to look to see if the Split will error if there is only a first name and no surname? I have looked at validating the form field but cannot see a way of ensuring it has a space between them.
Any ideas?
Split is not the problem. If the string doesn't have a space in it, Split won't throw an error, but will return an array with only one element. What you should do is check the upper bound of WordArray using the UBound function:
Dim WordArray, firstName, lastName
WordArray = Split(NameStr, " ")
firstName = WordArray(0)
If UBound(WordArray) >= 1 Then
lastName = WordArray(1)
Else
lastName = ""
End If
It's not really a fix, but I managed some "old school" VBA techniques and it has at least trapped the error:
<%
If Request.Form("user[username]") <> "" Then
NameStr = Request.Form("user[username]")
Dim WordArray
WordArray = Split(NameStr, " ")
On Error Resume Next
End If
%>
The On Error Resume Next just ignores the error and moves on. Dirty (just like ASP is, but it works!).

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

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!

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 />");

Resources