Question about control collection...

  • Thread starter Thread starter Unforgiven
  • Start date Start date
U

Unforgiven

I have an ASP.NET page with 14 different text box on it that gets data from
a SQL Server database. Please see the following code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)

If Not IsPostBack Then
'This works
'TextBox1.DataBind()
'TextBox2.DataBind()
'TextBox3.DataBind()
'TextBox4.DataBind()
'TextBox5.DataBind()
'TextBox6.DataBind()
'TextBox7.DataBind()
'TextBox8.DataBind()
'TextBox9.DataBind()
'TextBox10.DataBind()
'TextBox11.DataBind()
'TextBox12.DataBind()
'TextBox13.DataBind()
'TextBox14.DataBind()

'Why doesn't this work?
Dim c As Control

For Each c In Controls
If TypeOf c Is TextBox Then
c.DataBind()
End If
Next
End If
End Sub

Why does the top portion (that is commented out) work but the part that
loops through the collection does not? Is there something that I have to
reference gloablly? Is there something that I'm missing here?

Thanks,

-U

PS. I'm new to .NET
 
Unforgiven,

Because it is a webpage, it acts a little bit else than documented, however
when in your code the toppart works, (I do not see what you bind) than this
should do the job.

I hope this helps?

Cor
\\\
Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls
If TypeOf ctl Is TextBox Then
DirectCast(ctl, TextBox).databind
End If
Next
///
 
Cor Ligthert wrote:
: Unforgiven,
:
: Because it is a webpage, it acts a little bit else than documented,
: however when in your code the toppart works, (I do not see what you
: bind) than this should do the job.
:
: I hope this helps?
:
: Cor
: \\\
: Dim frm As Control = Me.FindControl("Form1")
: Dim ctl As Control
: For Each ctl In frm.Controls
: If TypeOf ctl Is TextBox Then
: DirectCast(ctl, TextBox).databind
: End If
: Next
: ///
:
:

Thanks Cor, that works. But here is something that confuses me. You have...

Dim frm As Control = Me.FindControl("Form1")

I don't have a form called Form1. The name of the page that I have is
default.aspx. Should I replace Form1 with default.aspx or leave it as it is?
And also, why does 'Me.FindControl("Form1")' work when I don't have a form
called 'Form1'?

Thanks,

-U

[snip]
 
Unforgiven
Thanks Cor, that works. But here is something that confuses me. You have...

Dim frm As Control = Me.FindControl("Form1")

I don't have a form called Form1. The name of the page that I have is
default.aspx. Should I replace Form1 with default.aspx or leave it as it is?
And also, why does 'Me.FindControl("Form1")' work when I don't have a form
called 'Form1'?

Look at the class name in top of your code, standard when you do not change
it that is Form1.

I think that is with you as well?

Cor
 

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

Back
Top