HowTo loop through HTML Table WebControls?

  • Thread starter Thread starter Andreas Klemt
  • Start date Start date
A

Andreas Klemt

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 said:
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.
 
Hello John,
and why does this not work?

For Each ctl As Control In Me.Controls ?

Thanks,
Andreas
 
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



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
 
Back
Top