Problem with FindConrol

  • Thread starter Thread starter Steven K
  • Start date Start date
S

Steven K

Hello,

I am trying to use the findcontrol method, but keep getting an error:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

I have tried:
Dim lblFind As String = CType(FindControl("lblSubmitter"), Label).Text
Dim lblFind As String = CType(Me.FindControl("lblSubmitter"), Label).Text
Dim lblFind As String = CType(Page.FindControl("lblSubmitter"), Label).Text

Any help with this would be appreciated
 
Where is the control you are trying to find.
IS it in a datagrid, or just on the page.

If it is on the page, you can just refer directly to it.

Dim lblFind as String = lblSubmitter.Text

If you are using CodeBehind the page, you'll have to reference the control
within the page class
protected lblSubmitter as Label
Then you can refer directly to it as above

If the control is is a datagrid, list or repeater, you'll need to make a Sub
with the DataGridCommandEventArg
E.G
Public Sub GetValue(Sender as Object, e as DataGridCommandEventArg)
Dim lblFind as String =
CType(e.Item.FindControl("lblSubmitter"), Label).Text
End Sub
 
Steven,

FindControl method searches the current naming container only. All 3
statement you've tried looked for the control in the page naming container.
If the label is not directly on the page, for example on a panel or in a
table, page's FindControl won't find it.

Eliyahu
 
I realized not too long ago that a Panel is not actually a naming
container.

The naming containers are:

CheckBoxList
DataGrid
DataGridItem
DataList
DataListItem
RadioButtonList
Repeater
RepeaterItem
TemplateControl (Page and UserControl)
 

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