Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to loop thru all textboxes on form?

Reply
Thread Tools

How to loop thru all textboxes on form?

 
 
VB Programmer
Guest
Posts: n/a
 
      01-04-2005
How can I loop thru all textboxes on a form?

I want to remove any single quotes in the data entry. I was thinking
something like this:

For Each c As Control In Me.Controls
Dim t As TextBox = CType(c, TextBox)
t.Text = t.Text.Replace("'", "")
Next

But the CType is giving me a 'Specified cast is not valid'.

Thanks.


 
Reply With Quote
 
 
 
 
nmosafi@gmail.com
Guest
Posts: n/a
 
      01-04-2005
Your page will contain many controls, so you have to check each
control's type before you can cast to a textbox. You can use the "Is"
operator - in C# it would look like:

if (c is TextBox) {
TextBox t = (TextBox) c;
t.Text = t.Text.Replace ("'", "");
}

but I dont know VB syntax.

An alternative would be to surround the statement in a try/catch
statement and catch the InvalidCastException and continue the loop,
however it's usually better to check for expected exceptions.

 
Reply With Quote
 
 
 
 
John Spiegel
Guest
Posts: n/a
 
      01-04-2005
Hey,

A couple ideas:

Use the GetType() method to then test whether or not it is of type TextBox,
only taking action if it is.

A bit more brute force would be to wrap it in a try...catch and check for
the invalid cast, but that's not too pretty.

- John

"VB Programmer" <Dont*NoSpam-Please*@jEmail.com> wrote in message
news:(E-Mail Removed)...
> How can I loop thru all textboxes on a form?
>
> I want to remove any single quotes in the data entry. I was thinking
> something like this:
>
> For Each c As Control In Me.Controls
> Dim t As TextBox = CType(c, TextBox)
> t.Text = t.Text.Replace("'", "")
> Next
>
> But the CType is giving me a 'Specified cast is not valid'.
>
> Thanks.
>
>



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      01-04-2005
What everyone else said is accurate, but I wanted to point out that you'll
need to recursively scan through all controls - your code doesn't show that
you are doing this (doesn't show that you aren't either).

private sub StripFromTextbox(parent as Control, searchFor as string,
replaceWith as string)
for each c as control in parent
if c.HasControls then
StripFromTextbox(c, searchFor, replaceWith)
end if
if c is TextBox then
dim t as textbox = ctype(c, textbox)
t.text = t.text.replace(searchFor, replaceWith)
end if
end sub

or something to that effect...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"VB Programmer" <Dont*NoSpam-Please*@jEmail.com> wrote in message
news:(E-Mail Removed)...
> How can I loop thru all textboxes on a form?
>
> I want to remove any single quotes in the data entry. I was thinking
> something like this:
>
> For Each c As Control In Me.Controls
> Dim t As TextBox = CType(c, TextBox)
> t.Text = t.Text.Replace("'", "")
> Next
>
> But the CType is giving me a 'Specified cast is not valid'.
>
> Thanks.
>
>



 
Reply With Quote
 
Saravana
Guest
Posts: n/a
 
      01-04-2005
check out this faq,
http://www.extremeexperts.com/Net/Ar...hControls.aspx

--
-Saravana
http://dotnetjunkies.com/WebLog/saravana/
www.ExtremeExperts.com



"VB Programmer" <Dont*NoSpam-Please*@jEmail.com> wrote in message
news:(E-Mail Removed)...
> How can I loop thru all textboxes on a form?
>
> I want to remove any single quotes in the data entry. I was thinking
> something like this:
>
> For Each c As Control In Me.Controls
> Dim t As TextBox = CType(c, TextBox)
> t.Text = t.Text.Replace("'", "")
> Next
>
> But the CType is giving me a 'Specified cast is not valid'.
>
> Thanks.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Order notes starting at C thru B rather than A thru G Max Williams Ruby 3 01-06-2009 11:37 AM
RDP thru Cisco VPN client and thru 501 Failure curttampa@gmail.com Cisco 21 08-26-2008 03:11 PM
Can't access Web Service thru DLL (but can thru Windows App) THTB ASP .Net Web Services 0 05-17-2007 06:43 PM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM



Advertisments