 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 4th, 2007, 05:30 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code Efficiency
Hi,
Using asp.net 1.1. and vb.net 2003 I have a form which contains a number of
textboxes which uses the replace function to remove any unwanted quotes
inputted by accident. Rather than using all of the code below, how could I
shorten this code, making it more efficient to produce the same results -
thanks ?
Code:
'Removing any unwanted quotation marks
txtName.Text = txtName.Text.Replace("""","")
txtAddress.Text = txtAddress.Text.Replace("""","")
txtTown.Text = txtTown.Text.Replace("""","")
txtCounty.Text = txtCounty.Text.Replace("""","")
txtPcode.Text = txtPcode.Text.Replace("""","")
txtCountry.Text= txtCountry.Text.Replace("""","")
txtEmail.Text = txtEmail.Text.Replace("""","")
txtSecQu.Text = txtSecQu.Text.Replace("""","")
txtSecAns.Text = txtSecAns.Text.Replace("""","")
txtUserid.Text = txtUserid.Text.Replace("""","")
txtPassword.Text = txtPassword.Text.Replace("""","")
|
|

August 4th, 2007, 05:42 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In code-behind, you have to do this thing. But if you like, you can also use javascript to check it on client side. for e.g. if user enter the " in textbox, undo the keyboard click event. But then user will not be able to enter " at all in textbox.
|
|

August 6th, 2007, 04:31 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, I don't agree with your solution. You are right that this is one of the other way to perform the same thing as rsm42 was doing but that will take longer only as it will try to find all controls. And for textboxes, it will replace the string. How efficiency improve by writing for loop and iterating through each control?
|
|

August 6th, 2007, 12:11 PM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks - tried the code :
For Each ctrl As Control In form.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = CType(ctrl, TextBox).Text.Replace("""","")
End If
Next
Notice that I added different quotes, that was working previously. No error produced but quotes not removed. Any ideas why ? if I use ("\"", ""); I get message advising double quote required at the end.
|
|

August 6th, 2007, 12:37 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have check your code, it's also giving correct output. Ok, try and change your code with that below:
For Each ctrl As Control In form.Controls
If TypeOf ctrl Is TextBox Then
Dim txt As TextBox = CType(ctrl, TextBox)
txt.Text = txt.Text.Replace("""", "")
End If
Next
|
|

August 7th, 2007, 12:23 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2006
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry but I don't understand y aren't you using expression validator?
Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"
|
|

August 7th, 2007, 07:18 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
An expression validator would not be a wise decision here because it is not acceptable to force the user to type and forbid them from using quotes. From a functional standpoint, that is a bad user experience and it is easier just to deal with this on the server side.
If by expression validator, you mean a RegEx replace, I have to disagree with this approach as well. By and large, RegEx replace's are slower then that of the string class and you add an unnecessary layer of complexity just to deal with a quote.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
================================================== =========
|
|

August 7th, 2007, 02:28 PM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks both.
I used the suggested code i.e.
For Each ctrl As Control In Form.Controls
If TypeOf ctrl Is TextBox Then
Dim txt As TextBox = CType(ctrl, TextBox)
txt.Text = txt.Text.Replace("""", "")
End If
Next
But this results in the following error :
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30390: 'System.Web.UI.Page.Private ReadOnly Property Form() As System.Web.UI.HtmlControls.HtmlForm' is not accessible in this context because it is 'Private'.
Source Error:
Line 242:
Line 243: For Each ctrl As Control In Form.Controls
Line 244: If TypeOf ctrl Is TextBox Then
Line 245: Dim txt As TextBox = CType(ctrl, TextBox)
I then altered the code, to the following :
For Each ctrl As Control In Page.Controls
If TypeOf ctrl Is TextBox Then
Dim txt As TextBox = CType(ctrl, TextBox)
txt.Text = txt.Text.Replace("""", "")
End If
Next
This resolves the error and goes to the form. Unfortunately, though it does not remove the quotes.
|
|
 |