Groups | Blog | Home
all groups > asp.net webcontrols > october 2003 >

asp.net webcontrols : Using an apostrophe in a textbox


Doug
10/31/2003 8:47:58 AM
I am creating an ASP.Net application that has a textbox
populated with a store name field from a SQL Server
database. I need to allow the user to change the name
string, and update the database. In several instances,
the name will contain an apostrophe (i.e. Macy's). In
the code behind, using the assignment: str =
textbox.text, the variable will contain the string
(Macys) without the apostrophe. How do I get the
apostrophe to show up? Thanks.

susie
10/31/2003 9:23:34 AM
Use string manipulation functions
'---
Sub ChangeBox( s As Object, e As EventArgs )
Dim a As String = txtBox.Text
Dim b As String
dim c as String
dim d as String
b = right(a, 1)
c=left(a,len(a)-1)
d=c+"'"+b
txtBox.Text=d
End Sub

'---------
<asp:TextBox
ID="txtBox"
AutoPostBack="True"
onTextChanged="ChangeBox"
Runat="Server"/>

The above code should work. You will need to change to
datasource for the textbox if it is populated from SQL
server.

Good luck.
[quoted text, click to view]
Doug
10/31/2003 9:49:00 AM
Thanks for the reply. That will work for the case I
mentioned, but there are other scenarios that are
different: A'Chen, Frank's Foods, Linens 'N Things, etc.
I cannot know how the users are going to want the store
names displayed, so I must be able to replicate an
apostrophe at any position in the string.

[quoted text, click to view]
vMike
10/31/2003 1:02:52 PM
You might want to look into
HttpUtility.HtmlEncode
and
HttpUtility.HtmlDecode


[quoted text, click to view]

Doug
10/31/2003 1:12:06 PM
I looked at that but don't see how that is going to
help. Somehow I am losing the apostrophe between the
textbox and the code behind .vb page. It is missing in
the locals window for textbox.text as soon as it comes up
there. If I type in 2 apostrophes they show up in locals.

[quoted text, click to view]
jose
10/31/2003 3:44:28 PM
so after assignment, str has Macys
without the apostrophe?
when debugging, does textbox.text contain the apostrophe?
I don't know if this will work for your case but I had
a somewhat similar problem when trying to update a text
field in a MS Access table. Access was reading the
apostrophe as a single quote and generating an error.
I used something like this to "double-up" the single quote.
string str = textbox.text.Replace("'", "''");


[quoted text, click to view]
Doug
11/3/2003 5:04:13 AM
The apostrophe exists in the textbox, but does not in
debugging when it will be assigned to a variable. I had
written almost that exact code to double up the
apostrophe to add to my update query, but without the
first apostrophe, I can't get a second one. Weird...

[quoted text, click to view]
Doug
11/3/2003 11:31:26 AM
It looks like either the page or project has become
corrupt. If I duplicate the functionality in a new
project, it works fine. Thanks for everyone's input.

Doug

[quoted text, click to view]
AddThis Social Bookmark Button