How To loop inside a HTML Table to find 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
 
K

Ken Cox [Microsoft MVP]

Hi Andreas,

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
 
A

Andreas Klemt

Hello Ken,
thanks for your answer.

But I wonder why I can do this in my code with ME.

Me.btn1.Enabled = false

but when I want to loop through my Controls in ME, it is not in there

For Each ctl As Control in Me.Controls
....## Here is no Me.btn1 ##...
Next

Thanks for any help,
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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top