how to make a "for each" in WebControls?

D

Dexter

Hello all,
I have webform1, and i need to make a bow to catch all textbox in my
webform1.
I tray this, but don't function.

Dim obj As System.Web.UI.WebControls.TextBox
Dim cont As Integer
For Each obj In Page.Controls.
If TypeOf obj Is System.Web.UI.WebControls.TextBox Then
obj.Text = "It is a Textbox"
End If
Next

Somebody know to make this?


Thanks


Dexter
 
G

Guest

Hi Dexter,

Make sure you are looking in the right collection for the TextBox and don't
be too specific with your initial object. Try the code below and let us know
if it helps?

Ken

<%@ Page Language="VB" trace="true" Debug="true" %>
<script runat="server">

Sub page_Load
Dim obj As Object
For Each obj In Page.Controls(1).controls
If TypeOf obj Is System.Web.UI.WebControls.TextBox Then
obj.Text = "It is a Textbox"
End If
Next
end sub

</script>
<html>
<head>
</head>
<body style="BACKGROUND: url(http://www.gc.ca/images/rollingflag.jpg) ivory
no-repeat center center">
<form runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
<!-- Insert content here -->
</form>
</body>
</html>
 
D

David P. Donahue

Will this also catch text boxes that are in containers in the specified
container? If not, is there a generic container object that can be used
to make something more recursive? I don't have the ability to test it
at the moment, but I was thinking something like:

Sub page_Load
changeText(Page.Controls(1).controls)
end sub

Sub changeText(ByVal myContainer As $CONTAINER_TYPE)
Dim obj As Object
For Each obj In Page.Controls(1).controls
If TypeOf obj Is System.Web.UI.WebControls.TextBox Then
obj.Text = "It is a Textbox"
ElseIf TypeOf obj Is $CONTAINER_TYPE Then
changeText(obj)
End If
Next
End Sub


What would $CONTAINER_TYPE be?


Regards,
David P. Donahue
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top