How to empty all textbox in a page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SFJzb2Z0IEluZm9ybcOhdGljYQ==?=

    How to empty all textbox in a page

    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.Tex tBox'.
    --
    Thanks in advance
    Hércules
    HRsoft Informática - Rio de Janeiro - Brasil
    Softwares Administrativos - Automação Comercial e Folha de Pagamento


  • Mark Rae [MVP]

    #2
    Re: How to empty all textbox in a page

    "HRsoft Informática" <HRsoftInformti ca@discussions. microsoft.comwr ote in
    message news:70E2116D-10E8-4A5B-AE4A-23D3FE99893B@mi crosoft.com...
    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.Tex tBox'.
    Yes, that's correct. Think about it...

    Your code says iterate through *every* control in the page's control
    collection, cast it to a TextBox (even if it isn't), and then clear its Text
    property... As soon as the For loop reaches a control which *isn't* a
    TextBox, the exception will be thrown.

    So...

    For Each objControl As Control In Me.Controls
    If GetType(objCont rol) = "TextBox" Then
    DirectCast(objC ontrol, TextBox).Text = ""
    End If
    Next

    Apologies if the above isn't syntactically correct - I never go anywhere
    near VB.NET...


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Patrice

      #3
      Re: How to empty all textbox in a page

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

      --
      Patrice

      "Mark Rae [MVP]" <mark@markNOSPA Mrae.neta écrit dans le message de groupe
      de discussion : uKhMjZO6IHA.206 4@TK2MSFTNGP02. phx.gbl...
      "HRsoft Informática" <HRsoftInformti ca@discussions. microsoft.comwr ote in
      message news:70E2116D-10E8-4A5B-AE4A-23D3FE99893B@mi crosoft.com...
      >
      >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.Te xtBox'.
      >
      Yes, that's correct. Think about it...
      >
      Your code says iterate through *every* control in the page's control
      collection, cast it to a TextBox (even if it isn't), and then clear its
      Text property... As soon as the For loop reaches a control which *isn't* a
      TextBox, the exception will be thrown.
      >
      So...
      >
      For Each objControl As Control In Me.Controls
      If GetType(objCont rol) = "TextBox" Then
      DirectCast(objC ontrol, TextBox).Text = ""
      End If
      Next
      >
      Apologies if the above isn't syntactically correct - I never go anywhere
      near VB.NET...
      >
      >
      --
      Mark Rae
      ASP.NET MVP
      http://www.markrae.net

      Comment

      • Mark Rae [MVP]

        #4
        Re: How to empty all textbox in a page

        "Patrice" <http://www.chez.com/scribe/wrote in message
        news:3DFC6649-CCBA-4E5B-937C-509C2CCF2537@mi crosoft.com...

        [top-posting corrected]
        >For Each objControl As Control In Me.Controls
        > If GetType(objCont rol) = "TextBox" Then
        > DirectCast(objC ontrol, 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


        Comment

        • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

          #5
          RE: How to empty all textbox in a page

          pretty simple:


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

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

          ......


          public delegate bool ControlWalkerMa tcher (Control ctl);
          public Control[] ControlWalker(C ontrol ctl, ControlWalkerMa tcher matcher)
          {
          ArrayList list = new ArrayList();
          if (matcher(ctl)) list.Add(ctl);
          for (int i=0; i < ctl.Controls.Co unt; ++i)
          {
          Control[] childList = ControlWalker(c tl.Controls[i],matcher);
          if (childList.Leng th 0) list.AddRange(c hildList);
          }
          return (Control[]) list.ToArray(ty peof(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.Tex tBox'.
          --
          Thanks in advance
          Hércules
          HRsoft Informática - Rio de Janeiro - Brasil
          Softwares Administrativos - Automação Comercial e Folha de Pagamento

          >

          Comment

          • =?Utf-8?B?SFJzb2Z0IEluZm9ybcOhdGljYQ==?=

            #6
            Re: How to empty all textbox in a page

            Dear friend

            Unfortunatelly, this syntax not compile:
            Error 78 Type 'objcontrol' is not
            defined. C:\inetpub\wwwr oot\AjaxControl ToolkitWebSite1 \clientes.aspx. vb 317 28 C:\...\AjaxCont rolToolkitWebSi te1\

            --
            Hércules
            HRsoft Informática - Rio de Janeiro - Brasil
            Softwares Administrativos - Automação Comercial e Folha de Pagamento




            "Mark Rae [MVP]" wrote:
            "Patrice" <http://www.chez.com/scribe/wrote in message
            news:3DFC6649-CCBA-4E5B-937C-509C2CCF2537@mi crosoft.com...
            >
            [top-posting corrected]
            >
            For Each objControl As Control In Me.Controls
            If GetType(objCont rol) = "TextBox" Then
            DirectCast(objC ontrol, 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

            >
            >

            Comment

            • Mark Rae [MVP]

              #7
              Re: How to empty all textbox in a page

              "HRsoft Informática" <HRsoftInformti ca@discussions. microsoft.comwr ote in
              message news:64CF96BB-FC52-4356-B8D4-A30258E0C7CD@mi crosoft.com...

              [top-posting corrected again]
              >For Each objControl As Control In Me.Controls
              > If GetType(objCont rol) = "TextBox" Then
              > DirectCast(objC ontrol, 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...
              >
              Unfortunatelly, this syntax not compile:
              Error 78 Type 'objcontrol' is not
              defined. C:\inetpub\wwwr oot\AjaxControl ToolkitWebSite1 \clientes.aspx. vb
              317 28 C:\...\AjaxCont rolToolkitWebSi te1\
              Apologies, try this instead:

              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
              Handles Me.Load
              For Each objControl As Control In Me.Controls
              If TypeOf (objControl) Is TextBox Then
              DirectCast(objC ontrol, TextBox).Text = ""
              End If
              Next
              End Sub


              --
              Mark Rae
              ASP.NET MVP


              Comment

              Working...