WebControls enumeration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vik

    #1

    WebControls enumeration

    Is there enumeration for the WebControls?

    Thanks.


  • John Timney \(Microsoft MVP\)

    #2
    Re: WebControls enumeration

    try something like this

    foreach (Control c in this.Controls)
    {
    if (c.GetType().To String().Equals ("System.Web.UI .WebControls.Te xtBox")
    {
    // ...do something...
    }
    }



    --
    Regards

    John Timney (Microsoft ASP.NET MVP)
    ----------------------------------------------
    <shameless_auth or_plug>
    Professional .NET for Java Developers with C#
    ISBN:1-861007-91-4
    Professional Windows Forms
    ISBN: 1861005547
    Professional JSP 2nd Edition
    ISBN: 1861004958
    Professional JSP
    ISBN: 1861003625
    Beginning JSP Web Development
    ISBN: 1861002092
    </shameless_autho r_plug>
    ----------------------------------------------

    "Vik" <viktorum@==yah oo.com==> wrote in message
    news:OQriV2kRDH A.1720@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Is there enumeration for the WebControls?
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Mark Friedman

      #3
      Re: WebControls enumeration

      If you're using VB try this:

      For Each (Control c in Controls)
      if TypeOf c is System.Web.UI.W ebControl Then
      // ...do something...
      Next

      -Mark
      "John Timney (Microsoft MVP)" <xyztimneyj@bti nternet.com> wrote in message
      news:OeDr3LlRDH A.1804@TK2MSFTN GP11.phx.gbl...[color=blue]
      > try something like this
      >
      > foreach (Control c in this.Controls)
      > {
      > if (c.GetType().To String().Equals ("System.Web.UI .WebControls.Te xtBox")
      > {
      > // ...do something...
      > }
      > }
      >
      >
      >
      > --
      > Regards
      >
      > John Timney (Microsoft ASP.NET MVP)
      > ----------------------------------------------
      > <shameless_auth or_plug>
      > Professional .NET for Java Developers with C#
      > ISBN:1-861007-91-4
      > Professional Windows Forms
      > ISBN: 1861005547
      > Professional JSP 2nd Edition
      > ISBN: 1861004958
      > Professional JSP
      > ISBN: 1861003625
      > Beginning JSP Web Development
      > ISBN: 1861002092
      > </shameless_autho r_plug>
      > ----------------------------------------------
      >
      > "Vik" <viktorum@==yah oo.com==> wrote in message
      > news:OQriV2kRDH A.1720@TK2MSFTN GP10.phx.gbl...[color=green]
      > > Is there enumeration for the WebControls?
      > >
      > > Thanks.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • bruce barker

        #4
        Re: WebControls enumeration

        actually you need to recurse, or you will miss controls in usercontrols,
        grids, etc.

        walkControls(th is.Controls);

        void walkControls (Control c)
        {
        foreach (Control child in this.Controls)
        {
        walkControl(chi ld);

        if
        (child.GetType( ).ToString().Eq uals("System.We b.UI.WebControl s.TextBox")
        {
        // ...do something...
        }
        }
        }
        }

        "John Timney (Microsoft MVP)" <xyztimneyj@bti nternet.com> wrote in message
        news:OeDr3LlRDH A.1804@TK2MSFTN GP11.phx.gbl...[color=blue]
        > try something like this
        >
        > foreach (Control c in this.Controls)
        > {
        > if (c.GetType().To String().Equals ("System.Web.UI .WebControls.Te xtBox")
        > {
        > // ...do something...
        > }
        > }
        >
        >
        >
        > --
        > Regards
        >
        > John Timney (Microsoft ASP.NET MVP)
        > ----------------------------------------------
        > <shameless_auth or_plug>
        > Professional .NET for Java Developers with C#
        > ISBN:1-861007-91-4
        > Professional Windows Forms
        > ISBN: 1861005547
        > Professional JSP 2nd Edition
        > ISBN: 1861004958
        > Professional JSP
        > ISBN: 1861003625
        > Beginning JSP Web Development
        > ISBN: 1861002092
        > </shameless_autho r_plug>
        > ----------------------------------------------
        >
        > "Vik" <viktorum@==yah oo.com==> wrote in message
        > news:OQriV2kRDH A.1720@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Is there enumeration for the WebControls?
        > >
        > > Thanks.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Vik

          #5
          Re: WebControls enumeration

          Thanks everybody.
          Originally I had code like this which worked fine:

          CtlType = ctl.GetType.Nam e
          Select Case CtlType
          Case "TextBox"
          ....
          Case "Label"
          ....
          End Select

          I changed it to:
          If TypeOf ctl Is TextBox Then
          ....
          ElseIf TypeOf ctl Is Label Then
          ....
          End If

          This code does not work because TypeOf ctl Is Label = True for example when
          CtlType="Compar eValidator".

          Vik

          "Ben" <mustbejoking@1 20spamsaday.con > wrote in message
          news:uHe%23xAnR DHA.2676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > ToString.Equals () or == is not very type safe, great for displaying to a
          > console or output file but in code you should really use "typeof" and[/color]
          "is",[color=blue]
          > after all the code knows these types already and you verify that the code[/color]
          is[color=blue]
          > correct at compile time. You need to itterate though the tree of controls
          > too as mentioned.
          >
          > Ben W
          >
          >
          > "John Timney (Microsoft MVP)" <xyztimneyj@bti nternet.com> wrote in message
          > news:OeDr3LlRDH A.1804@TK2MSFTN GP11.phx.gbl...[color=green]
          > > try something like this
          > >
          > > foreach (Control c in this.Controls)
          > > {
          > > if[/color][/color]
          (c.GetType().To String().Equals ("System.Web.UI .WebControls.Te xtBox")[color=blue][color=green]
          > > {
          > > // ...do something...
          > > }
          > > }
          > >
          > >
          > >
          > > --
          > > Regards
          > >
          > > John Timney (Microsoft ASP.NET MVP)
          > > ----------------------------------------------
          > > <shameless_auth or_plug>
          > > Professional .NET for Java Developers with C#
          > > ISBN:1-861007-91-4
          > > Professional Windows Forms
          > > ISBN: 1861005547
          > > Professional JSP 2nd Edition
          > > ISBN: 1861004958
          > > Professional JSP
          > > ISBN: 1861003625
          > > Beginning JSP Web Development
          > > ISBN: 1861002092
          > > </shameless_autho r_plug>
          > > ----------------------------------------------
          > >
          > > "Vik" <viktorum@==yah oo.com==> wrote in message
          > > news:OQriV2kRDH A.1720@TK2MSFTN GP10.phx.gbl...[color=darkred]
          > > > Is there enumeration for the WebControls?
          > > >
          > > > Thanks.
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...