HowTo loop through HTML Table WebControls?

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
 
J

John Saunders

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.
 
A

Andreas Klemt

Hello John,
and why does this not work?

For Each ctl As Control In Me.Controls ?

Thanks,
Andreas
 
K

Ken Cox [Microsoft MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top