code behind can't access user control

D

Davids

having a 100% working aspx page I wanted to create a user control from a
small DataList section (which has an ID="DataList1") of the page, so I
created an ascx file with the DataList and Register the control as should be
done. Now the DataList1 can't be found any more:
The name 'DataList1' does not exist in the current context

why's that??
 
D

Davids

if it helps, I see in the debugger that the control is not a child of the
page class... I had thought of using Page.FindControl() - is that really
neccesary (instead of somehow making the control a child of the page
class???)
 
J

John Saunders

Davids said:
having a 100% working aspx page I wanted to create a user control from a
small DataList section (which has an ID="DataList1") of the page, so I
created an ascx file with the DataList and Register the control as should
be done. Now the DataList1 can't be found any more:
The name 'DataList1' does not exist in the current context

why's that??

Because the .ascx file created a class. DataList1 is now a member of that
class.

John Saunders
 
D

Dave Fancher

To extend upon John's answer, you'll either need to expose the control
through a property of the class defined/by your .ascx OR change the
visibility modifier (VS.NET property: "Modifiers") of the control to public.
 
J

John Saunders

Dave Fancher said:
To extend upon John's answer, you'll either need to expose the control
through a property of the class defined/by your .ascx OR change the
visibility modifier (VS.NET property: "Modifiers") of the control to
public.

Neither of which is a good idea, in general.

You should probably reevaluate why you wanted the DataList to be in a .ascx
file. Exactly what do you want the .ascx file to look like to the pages its
living on? Among other things, you should probably hide the details of the
..ascx file from the pages using it. This implies that you do NOT want to
make the DataList directly accessible to the calling pages.

Keep in mind that you're in the realm of OO. You've created a class. Allow
it to hide its details from the world, and your code will be much more
reusable.

John Saunders
 
J

Johann MacDonagh

So you put a DataList inside a UserControl? In that case, the DataList won't
be a child control of the Page class. Instead, it is a child control of the
UserControl. You will have to use the FindControl method to use it. You can
emulate the coding style that you would normally use with Page child
controls by adding this line below the set of Protected WithEvents lines
(assuming you're writing this in VB.net):

Private DataList1 as System.Web.UI.WebControls.DataList =
MyUserControl.FindControl("DataList1")

Hooking up DataList events also becomes a problem. The best thing is to
handle the DataLists's events inside the UserControl. If you need to hook up
DataList events inside the Page class, you'll most likely have to expose the
DataList events as UserControl events. Reply back if this is the situation,
and I'll write up some working code for you!

Happy coding,
Johann MacDonagh
 
J

Johann MacDonagh

That's a good point. In some situations, where you have some core
functionality you want to reuse in several pages, it would be better off
creating a new server control that extends the functionality of the
DataList.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top