Find Control

S

shapper

Hello,

I created nested pages as follows:

Base.master
| ------ Global.master
| ------ TwoColumns.master
| ------ Default.aspx

In Global.master.vb I added a textbox in runtime:

1 Protected WithEvents tbUsername As New TextBox
2 cphGlobal.Controls.Add(tbUsername)

In Global.master.vb I also added a a button at runtime where I want to
access tbUsername when the button is clicked:

1 Private Sub ibSubmit_Click(ByVal sender As Object, ByVal e As
ImageClickEventArgs) Handles ibSubmit.Click
2
3 Dim ibSubmit As ImageButton = CType(sender, ImageButton)
4
5 Dim tbUsername As TextBox =
CType(Page.FindControl("tbUsername"), TextBox)
6 If String.IsNullOrEmpty(tbUsername.Text) Then
7 tbUsername.CssClass = "tbSystemError"
8 End If
9
10 End Sub

When I click the button I get the error, on line 6:

"Object reference not set to an instance of an object."

I tried many variations of my code. For example:

1 Dim tbUsername As TextBox = CType(Page.FindControl("tbUsername"),
TextBox)

However, I am not able to make this work.

Could someone, please, help me out?

I don't know what else to do.

Thanks,

Miguel
 
B

bruce barker

findcontrol does not do a recursive search. it only checks the current
control collection. do a find of cphGlobal.Controls. also its does not
appear that you are naming the control (set ID), so a default is assigned.

-- bruce (sqlwork.com)
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top