471,075 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

code behind can't access user control

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??
Nov 19 '05 #1
6 8498
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???)

"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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??

Nov 19 '05 #2
"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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
Nov 19 '05 #3
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.

--
Dave Fancher
http://davefancher.blogspot.com
"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OK*************@TK2MSFTNGP11.phx.gbl...
"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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

Nov 19 '05 #4
"Dave Fancher" <ei*****@comcast.net> wrote in message
news:RM********************@comcast.com...
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

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OK*************@TK2MSFTNGP11.phx.gbl...
"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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


Nov 19 '05 #5
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

"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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???)

"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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??


Nov 19 '05 #6
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.

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:ur**************@TK2MSFTNGP10.phx.gbl...
"Dave Fancher" <ei*****@comcast.net> wrote in message
news:RM********************@comcast.com...
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

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OK*************@TK2MSFTNGP11.phx.gbl...
"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Brian W | last post: by
6 posts views Thread by William Parker | last post: by
3 posts views Thread by Ryan Taylor | last post: by
1 post views Thread by zhuang | last post: by
2 posts views Thread by rn5a | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.