FindControl() on ASP.NET UserControl Objects

G

Guest

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
 
G

Guest

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/ShowPost.aspx?PostID=1563704&SiteID=1
Hope this helps
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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

Guest

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
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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

Steven Cheng[MSFT]

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

Guest

Steven -

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

Thanks.

Alex
 
S

Steven Cheng[MSFT]

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

Steven Cheng[MSFT]

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

Guest

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
 
S

Steven Cheng[MSFT]

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top