|
-
Apr 26th, 2005, 12:26 PM
#1
Looping through controls (vb.net)
I'm new to ASP.NET. I've been asked to convert one of my windows apps into an asp page. I would like to loop through the controls on my page, so I thought using a similar syntax would work, but it doesnt. Here is an example of the code I'm trying to use:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each oControl As Control In Page.Controls
If TypeOf oControl Is TextBox Then
Dim txtbox As TextBox = CType(oControl, TextBox)
txtbox.Text = Date.Now.ToString
End If
Next
End Sub
-
Apr 26th, 2005, 01:21 PM
#2
Re: Looping through controls (vb.net)
I got it working with:
VB Code:
Private Sub setdatetime(ByVal gui As Control)
For Each ocontrol As Control In Page.Controls
If TypeOf ocontrol Is TextBox Then
Dim txtbox As TextBox = CType(ocontrol, TextBox)
txtbox.Text = Date.Now.ToString
Else
setdatetime(ocontrol)
End If
Next
End Sub
I wrote all the controls that aren't textboxes out to a variable. Form1 showed up as a control, but I can't loop through Form1.Controls, as it hasn't been declared. Any ideas how to declare Form1, so I don't have to recursivly search through all the controls?
-
Apr 26th, 2005, 04:19 PM
#3
I wonder how many charact
Re: Looping through controls (vb.net)
You can simply put this at the top of your code-behind:
VB Code:
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
then use:
VB Code:
For Each ocontrol As Control In Form1.Controls
-
Apr 26th, 2005, 07:43 PM
#4
Frenzied Member
Re: Looping through controls (vb.net)
Yeah, that little Page.Form.Controls thing is annoying....
Magiaus
If I helped give me some points.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|