FindControl inside of a webUserControl

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.
 
S

Stan

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")
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top