473,513 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FindControl() on ASP.NET UserControl Objects

Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex

May 13 '07 #1
14 3181
Hi,
For declarative inclusion of control in the page, you used @Register
directive at the top of the page. But for programatic there is going to be a
change. You will use @Reference directive. This directive takaes only one
attribute, Page or Control. The value of this attribute specifies the file
that contains the control or the page that this page should link to during
dynamic compliation. This step is very important, otherwise your will get
Compiler Error CS0246 indicating that class name or type not found.
<%@ Reference Control="./controls/SiteHeader.ascx"%>
For more details refer following link:
http://forums.microsoft.com/MSDN/Sho...63704&SiteID=1
Hope this helps
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alex Maghen" wrote:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex
May 13 '07 #2
Manish -

Thanks for the help... but I still need more. So I placed the
<%@ Reference Control="./Controls/GalleryThumb.ascx"%>
directive at the top of the ASPX. That ASPX has a C# code-behind .CS page.
I'm still getting the same error from the .CS page. Is there something
specific I have to do in the actual .CS file to make the type available, or
should the "Reference" directive from the ASPC make the type available in its
code-behind as well?

Alex

"Manish Bafna" wrote:
Hi,
For declarative inclusion of control in the page, you used @Register
directive at the top of the page. But for programatic there is going to be a
change. You will use @Reference directive. This directive takaes only one
attribute, Page or Control. The value of this attribute specifies the file
that contains the control or the page that this page should link to during
dynamic compliation. This step is very important, otherwise your will get
Compiler Error CS0246 indicating that class name or type not found.
<%@ Reference Control="./controls/SiteHeader.ascx"%>
For more details refer following link:
http://forums.microsoft.com/MSDN/Sho...63704&SiteID=1
Hope this helps
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alex Maghen" wrote:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex
May 13 '07 #3
One more thing, Manish -

I tried building an ASPX WITHOUT a code-behind and then, it worked. Even
more, once it worked there, I could run the other file WITH the code-behind
and that one worked too (I guess because the ASCX had already successfully
compiled). So now, how do I make it work WITH the code-behind permanenetly?

Alex
"Manish Bafna" wrote:
Hi,
For declarative inclusion of control in the page, you used @Register
directive at the top of the page. But for programatic there is going to be a
change. You will use @Reference directive. This directive takaes only one
attribute, Page or Control. The value of this attribute specifies the file
that contains the control or the page that this page should link to during
dynamic compliation. This step is very important, otherwise your will get
Compiler Error CS0246 indicating that class name or type not found.
<%@ Reference Control="./controls/SiteHeader.ascx"%>
For more details refer following link:
http://forums.microsoft.com/MSDN/Sho...63704&SiteID=1
Hope this helps
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alex Maghen" wrote:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex
May 13 '07 #4
Hi,
I think you are calling this findcontrol in page load method.It is not fix
in which order the controls are loaded.You need to change your logic so that
you call findcontrol method in some other method.Also in following code:
MyUC C = (MyUC)this.FindControl("SomeID");
MyUC should be name of usercontrol class.
i am sure this.FindControl("SomeID") is returning null reference.Please try
to call it in some other methods and see if it works.
Hope this helps.
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alex Maghen" wrote:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex
May 14 '07 #5
Manish -

Hmmm... well, as I said in my last post, this is working perfectly in an
ASPX page where there is no code-behind. It is only when I put the
FindControl() logic in a code-behind page that I get the compile error. And
it *is* a compile (rather than null-reference) error. So it's really about it
being able to find the type, "MyUC" during compilation.

I guess I don't really understand what happens with code-behind pages. They
seem to act as one "class" where I don't, for example, have to declare
controls tat I've inserted on the ASPX page when I use them in the .CS
code-behind. But in this case, the <@Reference that I do on the ASPX doesn't
seem to "make it" into the code-behind .CS file.

What do you think?

Alex

"Manish Bafna" wrote:
Hi,
I think you are calling this findcontrol in page load method.It is not fix
in which order the controls are loaded.You need to change your logic so that
you call findcontrol method in some other method.Also in following code:
MyUC C = (MyUC)this.FindControl("SomeID");
MyUC should be name of usercontrol class.
i am sure this.FindControl("SomeID") is returning null reference.Please try
to call it in some other methods and see if it works.
Hope this helps.
--
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alex Maghen" wrote:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of
that control on a Page ("Defaul.aspx"). The control works fine.

Now, I want to be able to use "FindControl()" from within my Default.aspx as
follows:

MyUC C = (MyUC)this.FindControl("SomeID");

When I try to run this, I get error:
"The type or namespace name 'MyUC' could not be found (are you missing a
using directive or an assembly reference?)"

This error is on that "FindControl()" line. Also, in the actual VS editor,
in the Default.aspx code, the references to the type "MyUC" *do* show up in
green and seem to be recognized. But when I run it, I get this error.

So what I'm confused about is, how do I refernce this new control type I've
created ("MyUC").

One more note: None of my files (the ASPX pages, MyUC.ascx, etc.) have
namespaces. I just created the files from within VS. Is there some default
namespace I have to use or something?

Much appreciated!

Alex
May 14 '07 #6
Hi Alex,

For the type missing issue you met, I agree that adding a reference
directive in your aspx page(which use the usercontrols) is the reasonable
approach. I'm not sure about how your codebehind is written and whether is
your ascx usercontrol put(in a sub folder?). I think there are some other
things you can check:

1. Try using the "@Reference" directive with "VirtualPath" attribute to
specify the usercontrol reference. e.g.

=================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcode.aspx.cs"
Inherits="test_testcode" %>

<%@ Register Src="../uc/HelloWorldUC.ascx" TagName="HelloWorldUC"
TagPrefix="uc1" %>

<%@ Reference VirtualPath="~/uc/HelloWorldUC.ascx" %>
====================

Then, in you codebehind use VS IDE intellisense to get the usercontrol
instance to see whether you can correctly use intellisense to navigate all
the public members of the usercontro class.

2. ASP.NET 2.0 Will use folder hierarchy to naming the usercontrol's
codebehind class. e.g.

if you have a usercontrol named "HelloWorldUC.ascx" and put it in a sub
folder, the codebehind class name will become:

public partial class uc_HelloWorldUC : System.Web.UI.UserControl
{

So you can also verify whether you've used the correct class name(identical
to the one in the ascx usercontrol's codebehind file) in your main page.
If you feel necessary, I can also send you a set of test page and control
for your reference.

Please feel free to post here if you have any questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
May 14 '07 #7
STeven, when I look in the code-behind for the user control, despite the fact
that the control is in a folder named "Controls", I have named the Class
"MyUC" (with no underscores or anything). I should be able to do that as long
as it's unique.

What's so weired is that sometimes the thing compiles and other times it
doesn't. Sometimes the Intellisense comes up and other times it doesn't.
Seems to be based on whether I've "touched" the files or not or something. I
have an ASPX which doesn't have a code-behind and the some code compiles
perfectly that way. If I run that page first then go back to VS and set the
ASPX *with* the code-behind as the Start Up page, then even *that* page runs
- unless I then go and touch the ASCX and then it stops working.

I tried the "VirtualPath" thing you suggested and still this is happening.

Yes, I would VERY much appreciate ti if you would create a very simple
sample with this structure:
/Default.aspx
/Default.aspx.cs (code-behind) - which uses the "MyUC" type in the code
somewhere
/Controls/MyUC.ascx
/Controls/MyUC.ascx.cs (code-behind)

Your help is MUCH appreciated!

Alex
"Steven Cheng[MSFT]" wrote:
Hi Alex,

For the type missing issue you met, I agree that adding a reference
directive in your aspx page(which use the usercontrols) is the reasonable
approach. I'm not sure about how your codebehind is written and whether is
your ascx usercontrol put(in a sub folder?). I think there are some other
things you can check:

1. Try using the "@Reference" directive with "VirtualPath" attribute to
specify the usercontrol reference. e.g.

=================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcode.aspx.cs"
Inherits="test_testcode" %>

<%@ Register Src="../uc/HelloWorldUC.ascx" TagName="HelloWorldUC"
TagPrefix="uc1" %>

<%@ Reference VirtualPath="~/uc/HelloWorldUC.ascx" %>
====================

Then, in you codebehind use VS IDE intellisense to get the usercontrol
instance to see whether you can correctly use intellisense to navigate all
the public members of the usercontro class.

2. ASP.NET 2.0 Will use folder hierarchy to naming the usercontrol's
codebehind class. e.g.

if you have a usercontrol named "HelloWorldUC.ascx" and put it in a sub
folder, the codebehind class name will become:

public partial class uc_HelloWorldUC : System.Web.UI.UserControl
{

So you can also verify whether you've used the correct class name(identical
to the one in the ascx usercontrol's codebehind file) in your main page.
If you feel necessary, I can also send you a set of test page and control
for your reference.

Please feel free to post here if you have any questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
May 14 '07 #8
Thanks for your reply Alex,

Sure, I'll be glad to provide you a test web application. Please ping me at
("stcheng" + "@microsoft.com") so that I can send the project to your
proper mail address.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 15 '07 #9
Hi Alex,

How are you doing on this issue? Have you sent me the email for the test
project, if you need a one, please feel free to ping me at "stcheng" +
"@microsoft.com" so that I can send the test project to you via the proper
address.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 17 '07 #10
Steven -

Sent you an email. My address is alex -at-sign- maghen.com. Please do send
what you have!

Thanks.

Alex
"Steven Cheng[MSFT]" wrote:
Hi Alex,

How are you doing on this issue? Have you sent me the email for the test
project, if you need a one, please feel free to ping me at "stcheng" +
"@microsoft.com" so that I can send the test project to you via the proper
address.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 20 '07 #11
Thanks Alex,

I've got it and replied you with the test project in attachment.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 21 '07 #12
Hi Alex,

Have you got the test project I sent or any progress on this? If there is
anything else we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
May 23 '07 #13
Steven -

Thanks for sending it to me. Your sample works perfectly. I'm trying to
figure out what's different between yours and mine. For some reason, mine
doesn't just JIT compile. I have to run as the Start Page a page that doesn't
have code-behind. That will allow it to compile. *Then*, I can run the other
pages that *do* have Code-Behind. I'll write back tomorrow when I've had a
few minutes to play with this some more.

Thanks!

Alex

"Steven Cheng[MSFT]" wrote:
Hi Alex,

Have you got the test project I sent or any progress on this? If there is
anything else we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
May 23 '07 #14
Hi Alex,

Sure, if you need any further help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 24 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3157
by: Dave McCracken | last post by:
I am hosting .Net UserControls in HTML pages with Object Tags. These UserControls are clients of remotable objects that run on the same machine. The remote objects execute callbacks via sponsor...
1
3669
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
3
3948
by: ZhangZQ | last post by:
Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl" in an Assembly "UI.dll", now I create an Application project and want to use the "UserControl1" in another AppDomain, ...
1
12045
by: Michael Murschell | last post by:
If I call Page.FindControl() on an aspx, it finds the control, but if I call it on an ASCX, it does not. Why not? And how would I call it?
3
3023
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
3
1110
by: Brad Roberts | last post by:
I have a web page that makes extensive use of FindControl in the Page.Load event to dynamicly populate label controls. Worked fine in 1.1 but I get a "object not set to an instance...or not found"...
2
1053
by: Mr. SweatyFinger | last post by:
What gives? These two controls are right next to each other in a usercontrol, yet in the code-behind Can't find: Dim summaryx As HiddenField = FormView1.FindControl("Summary2")
5
1808
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
5
4104
by: John Kotuby | last post by:
Hi all, After more than a year programming with ASP.NET 2.0 and VB I am still finding it difficult to leave some habits from classic ASP behind. this is particularly true with cross-page posting....
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.