Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
 
Old August 4th, 2007, 05:30 AM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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("""","")

 
Old August 4th, 2007, 05:42 AM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

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.
 
Old August 4th, 2007, 12:13 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You can do this:

foreach(Control ctrl in <container control>.Controls)
{
  if(ctrl is Textbox)
  {
       Textbox t = (TextBox)ctrl;
       t.Text = t.Text.Replace("\"", "");
  }
}

the syntax for VB would be similar.

================================================== =========
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

================================================== =========
 
Old August 6th, 2007, 04:31 AM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

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?
 
Old August 6th, 2007, 12:11 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.



 
Old August 6th, 2007, 12:37 PM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

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
 
Old August 6th, 2007, 03:55 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In C#, when you want to match a literal " you have to escape (\) it, in VB you do not.

So
C#
txt.Text = txt.Text.Replace("\"", "");
VB
txt.Text = txt.Text.Replace("""", "")


================================================== =========
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

================================================== =========
 
Old August 7th, 2007, 12:23 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Amman, , Jordan.
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"
 
Old August 7th, 2007, 07:18 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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

================================================== =========
 
Old August 7th, 2007, 02:28 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Efficiency Kev_Akas PHP Databases 1 May 13th, 2008 09:24 AM
code efficiency sxstat90 SQL Language 1 March 8th, 2005 04:42 PM
efficiency qn - writing and saving xml file ak Classic ASP XML 1 March 2nd, 2004 05:26 PM
Function Efficiency Ben Access VBA 3 November 20th, 2003 04:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.