How to loop thru all textboxes on form?

Discussion in 'ASP .Net' started by VB Programmer, Jan 4, 2005.

  1. 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.
     
    VB Programmer, Jan 4, 2005
    #1
    1. Advertising

  2. VB Programmer

    Guest

    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.
     
    , Jan 4, 2005
    #2
    1. Advertising

  3. VB Programmer

    John Spiegel Guest

    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:...
    > 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.
    >
    >
     
    John Spiegel, Jan 4, 2005
    #3
  4. VB Programmer

    Karl Seguin Guest

    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:...
    > 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.
    >
    >
     
    Karl Seguin, Jan 4, 2005
    #4
  5. VB Programmer

    Saravana Guest

    check out this faq,
    http://www.extremeexperts.com/Net/Articles/LoopingthroughControls.aspx

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



    "VB Programmer" <Dont*NoSpam-Please*@jEmail.com> wrote in message
    news:...
    > 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.
    >
    >
     
    Saravana, Jan 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    625
    benben
    Jan 31, 2006
  2. THTB
    Replies:
    0
    Views:
    217
  3. Max Williams
    Replies:
    3
    Views:
    191
    Robert Klemme
    Jan 6, 2009
  4. Jeremy Langworthy

    Need to loop thru form elements

    Jeremy Langworthy, Aug 21, 2003, in forum: Javascript
    Replies:
    1
    Views:
    92
    steve stevo
    Aug 21, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    449
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page