HowTo loop through HTML Table WebControls?

Discussion in 'ASP .Net' started by Andreas Klemt, Aug 22, 2003.

  1. Hello,
    I have this:

    <table id="myTable" runat="server">
    <tr>
    <td><asp:label id="lblText" runat="server"></asp:label>
    <asp:button id="btn1" runat="server"></asp:button>
    <asp:button id="btn2" runat="server"></asp:button>
    <asp:button id="btn3" runat="server"></asp:button>
    </td>
    </tr>
    </table>


    Now I want to loop through like this:

    For Each ctl As Control In Me.myTable.Controls
    If TypeOf ctl Is Button Then
    CType(ctl, Button).Enabled = False
    End If
    Next

    But this doesn't work because in Me.myTable.Controls
    there is only 1 Control. Why is this wrong and what is the correct way?

    Thanks in advance,
    Andreas
     
    Andreas Klemt, Aug 22, 2003
    #1
    1. Advertising

  2. "Andreas Klemt" <> wrote in message
    news:...
    > Hello,
    > I have this:
    >
    > <table id="myTable" runat="server">
    > <tr>
    > <td><asp:label id="lblText" runat="server"></asp:label>
    > <asp:button id="btn1" runat="server"></asp:button>
    > <asp:button id="btn2" runat="server"></asp:button>
    > <asp:button id="btn3" runat="server"></asp:button>
    > </td>
    > </tr>
    > </table>
    >
    >
    > Now I want to loop through like this:
    >
    > For Each ctl As Control In Me.myTable.Controls
    > If TypeOf ctl Is Button Then
    > CType(ctl, Button).Enabled = False
    > End If
    > Next
    >
    > But this doesn't work because in Me.myTable.Controls
    > there is only 1 Control. Why is this wrong and what is the correct way?


    Andreas,

    If you looked, you'd see what the type of the one control is. That will tell
    you what's going on.
    --
    John Saunders
    Internet Engineer
     
    John Saunders, Aug 22, 2003
    #2
    1. Advertising

  3. Hello John,
    and why does this not work?

    For Each ctl As Control In Me.Controls ?

    Thanks,
    Andreas


    "John Saunders" <> schrieb im Newsbeitrag
    news:...
    > "Andreas Klemt" <> wrote in message
    > news:...
    > > Hello,
    > > I have this:
    > >
    > > <table id="myTable" runat="server">
    > > <tr>
    > > <td><asp:label id="lblText" runat="server"></asp:label>
    > > <asp:button id="btn1" runat="server"></asp:button>
    > > <asp:button id="btn2" runat="server"></asp:button>
    > > <asp:button id="btn3" runat="server"></asp:button>
    > > </td>
    > > </tr>
    > > </table>
    > >
    > >
    > > Now I want to loop through like this:
    > >
    > > For Each ctl As Control In Me.myTable.Controls
    > > If TypeOf ctl Is Button Then
    > > CType(ctl, Button).Enabled = False
    > > End If
    > > Next
    > >
    > > But this doesn't work because in Me.myTable.Controls
    > > there is only 1 Control. Why is this wrong and what is the correct way?

    >
    > Andreas,
    >
    > If you looked, you'd see what the type of the one control is. That will

    tell
    > you what's going on.
    > --
    > John Saunders
    > Internet Engineer
    >
    >
    >
     
    Andreas Klemt, Aug 22, 2003
    #3
  4. It would be better to keep a question to one newsgroup. Here's the answer I
    posted in the other group:

    Don't forget that your buttons are inside a table which has its own controls,
    so you have to dig a little deeper before you start looping for the buttons.
    You might want to use FindControl to get closer to the action.

    It helps to turn tracing on in a page to see where controls are really nested.

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ctl As Control
    Dim btn As System.Web.UI.WebControls.Button
    For Each ctl In myTable.Controls(0).Controls(0).Controls
    If TypeOf ctl Is System.Web.UI.WebControls.Button Then
    btn = ctl
    btn.Enabled = False
    End If
    Next
    End Sub

    Does this help>

    Ken MVP [ASP.NET]


    --
    Microsoft MVPs have a question for *you*: Are you patched against the Worm?
    http://www.microsoft.com/security/security_bulletins/ms03-026.asp



    "Andreas Klemt" <> wrote in message
    news:...
    Hello,
    I have this:

    <table id="myTable" runat="server">
    <tr>
    <td><asp:label id="lblText" runat="server"></asp:label>
    <asp:button id="btn1" runat="server"></asp:button>
    <asp:button id="btn2" runat="server"></asp:button>
    <asp:button id="btn3" runat="server"></asp:button>
    </td>
    </tr>
    </table>


    Now I want to loop through like this:

    For Each ctl As Control In Me.myTable.Controls
    If TypeOf ctl Is Button Then
    CType(ctl, Button).Enabled = False
    End If
    Next

    But this doesn't work because in Me.myTable.Controls
    there is only 1 Control. Why is this wrong and what is the correct way?

    Thanks in advance,
    Andreas
     
    Ken Cox [Microsoft MVP], Aug 22, 2003
    #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. Don Wash
    Replies:
    2
    Views:
    4,799
    Don Wash
    Sep 7, 2004
  2. Roedy Green
    Replies:
    3
    Views:
    454
    Mike Schilling
    Sep 13, 2008
  3. Andreas Klemt

    How To loop inside a HTML Table to find WebControls?

    Andreas Klemt, Aug 22, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    282
    Andreas Klemt
    Aug 22, 2003
  4. Calvin KD

    Newbie: Loop through WebControls on a form - C#

    Calvin KD, Dec 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    228
    mortb
    Dec 9, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    444
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page