FindControl inside of a webUserControl

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

I have 2 web user controls on a page. The first one is SearchNewsArchives
which contains a form for searching through the news archive. The second one
is called NewsArchive which is the control that has the list of news
articles in it. In the NewsArchives control there is a DataList called
NewsDataList. What I want to do is this: From inside the SearchNewsArchives
control I have a search button. In its click event I want to find the
NewsDataList inside the NewsArchives control and reasign its datasource.
When I did the following code in the search button click event, I got an
'object set to a null reference' error. Is there a way to fix this?

'This code appears in SearchNewsArchives.ascx
dim NewsArchive as News = DirectCast(Page.FindControl("NewsArchive"), News)
dim NewsList as DataList =
DirectCast(NewsArchives.FindControl("NewsDataList"), DataList) 'The compiler
doesn't like this line and says NewsDataList doesn't exist.
 
I have 2 web user controls on a page. The first one is SearchNewsArchives
which contains a form for searching through the news archive. The second one
is called NewsArchive which is the control that has the list of news
articles in it. In the NewsArchives control there is a DataList called
NewsDataList. What I want to do is this: From inside the SearchNewsArchives
control I have a search button. In its click event I want to find the
NewsDataList inside the NewsArchives control and reasign its datasource.
When I did the following code in the search button click event, I got an
'object set to a null reference' error. Is there a way to fix this?

'This code appears in SearchNewsArchives.ascx
dim NewsArchive as News = DirectCast(Page.FindControl("NewsArchive"), News)
dim NewsList as DataList =
DirectCast(NewsArchives.FindControl("NewsDataList"), DataList) 'The compiler
doesn't like this line and says NewsDataList doesn't exist.

Hi AndyB

I am surprised that your code even compiles let alone runs. Try the
following instead:

dim NewsArchive as UserControl = Page.FindControl("NewsArchive")
dim NewsList as DataList = NewsArchive.FindControl("NewsDataList")
 
Back
Top