How to empty all textbox in a page

Discussion in 'ASP .Net' started by HRsoft Informática, Jul 18, 2008.

  1. I Have a page (clientes.aspx), inside a masterpage
    I have some textbox, and when the user clicks the button 'Cancel', I need to
    empty all controls. I tried this, with runtine error:

    For Each txtControl As TextBox In Me.Controls
    txtControl.Text = ""
    Next

    error message in runtime:
    can't convert as object of type 'ASP.masterpage_master' in type
    'System.Web.UI.WebControls.TextBox'.
    --
    Thanks in advance
    Hércules
    HRsoft Informática - Rio de Janeiro - Brasil
    http://www.hrsoft.com.br
     
    HRsoft Informática, Jul 18, 2008
    #1
    1. Advertising

  2. HRsoft Informática

    David Wier Guest

    One inexpensive solution is a new control called ControlGrouper
    You can blank all the text from all textboxes grouped with it in 1-2 lines,
    plus control other properties like enabled, visible, width
    http://cg.augustwind.com

    David Wier
    http://aspnet101.com
    http://cg.augustwind.com - ControlGrouper (ASP.Net Control) - download and
    register for contest prizes
    http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    bloated markup


    "HRsoft Informática" <> wrote in
    message news:...
    >I Have a page (clientes.aspx), inside a masterpage
    > I have some textbox, and when the user clicks the button 'Cancel', I need
    > to
    > empty all controls. I tried this, with runtine error:
    >
    > For Each txtControl As TextBox In Me.Controls
    > txtControl.Text = ""
    > Next
    >
    > error message in runtime:
    > can't convert as object of type 'ASP.masterpage_master' in type
    > 'System.Web.UI.WebControls.TextBox'.
    > --
    > Thanks in advance
    > Hércules
    > HRsoft Informática - Rio de Janeiro - Brasil
    > http://www.hrsoft.com.br
    >
     
    David Wier, Jul 18, 2008
    #2
    1. Advertising

  3. HRsoft Informática

    bruce barker Guest

    pretty simple:


    // find all textbox controls on page
    Control[] list = ControlWalker(this, ctl => ctl is TextBox);

    // clear text
    foreach (Control ctl in list)
    {
    ((TextBox) ctl).Text = "";
    }

    ......


    public delegate bool ControlWalkerMatcher (Control ctl);
    public Control[] ControlWalker(Control ctl, ControlWalkerMatcher matcher)
    {
    ArrayList list = new ArrayList();
    if (matcher(ctl)) list.Add(ctl);
    for (int i=0; i < ctl.Controls.Count; ++i)
    {
    Control[] childList = ControlWalker(ctl.Controls,matcher);
    if (childList.Length > 0) list.AddRange(childList);
    }
    return (Control[]) list.ToArray(typeof(Control));
    }





    -- bruce (sqlwork.com)


    "HRsoft Informática" wrote:

    > I Have a page (clientes.aspx), inside a masterpage
    > I have some textbox, and when the user clicks the button 'Cancel', I need to
    > empty all controls. I tried this, with runtine error:
    >
    > For Each txtControl As TextBox In Me.Controls
    > txtControl.Text = ""
    > Next
    >
    > error message in runtime:
    > can't convert as object of type 'ASP.masterpage_master' in type
    > 'System.Web.UI.WebControls.TextBox'.
    > --
    > Thanks in advance
    > Hércules
    > HRsoft Informática - Rio de Janeiro - Brasil
    > http://www.hrsoft.com.br
    >
     
    bruce barker, Jul 18, 2008
    #3
  4. Dear friend

    Unfortunatelly, this syntax not compile:
    Error 78 Type 'objcontrol' is not
    defined. C:\inetpub\wwwroot\AjaxControlToolkitWebSite1\clientes.aspx.vb 317 28 C:\...\AjaxControlToolkitWebSite1\

    --
    Hércules
    HRsoft Informática - Rio de Janeiro - Brasil
    http://www.hrsoft.com.br



    "Mark Rae [MVP]" wrote:

    > "Patrice" <http://www.chez.com/scribe/> wrote in message
    > news:...
    >
    > [top-posting corrected]
    >
    > >> For Each objControl As Control In Me.Controls
    > >> If GetType(objControl) = "TextBox" Then
    > >> DirectCast(objControl, TextBox).Text = ""
    > >> End If
    > >> Next
    > >>
    > >> Apologies if the above isn't syntactically correct - I never go anywhere
    > >> near VB.NET...

    > >
    > > Plus you may have to recurse if you are on a container that holds
    > > textboxes...

    >
    > Yes, that's true - I should have mentioned that...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
     
    HRsoft Informática, Jul 18, 2008
    #4
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,039
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    820
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    582
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    665
    Jeremy Bowers
    May 4, 2005
  5. Steve
    Replies:
    4
    Views:
    217
    Steve
    Feb 7, 2006
Loading...

Share This Page