views:

310

answers:

3

Hi,

I have a default.aspx file and 2 user controls.

Code for user control 1

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<%@ Register Src="~/WebUserControl2.ascx" TagName="wc1" TagPrefix="asp2" %>

<asp2:wc1 ID="control1" runat="server" />

Code for user control 2

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>

Why can't I access user control 1 from my source code in user control 2?

protected void Page_Load(object sender, EventArgs e)
    {
        WebUserControl //Doesn't work
    }
A: 

You will need to create a public-property in usercontrol2 of type usercontrol1. In a page that will host the instances of both the controls, assign the reference of usercontrol1 to the property of usercontrol2 which is of type usercontrol1.

The reason why you can't access is, you need an instance of userocntrol1 in usercontrol2. This problem will be solved using above mentioned approach.

this. __curious_geek
I tried this but it doesn't work. For some reason, any control or page i create seems to be hidden from the 'outside world'. I can't even access WebUserControl from my Default.aspx.cs file.Something may be wrong with vs
Sir Psycho
It has to work. which version on VS are you using ? you have a website or web application project ? Also check-out all other components in your website are compiled properly. Sometimes I've observed that some the page may not have any error but it might not function as desired due to an error in some other part of the website. Please check if your website compiles completely without any error.
this. __curious_geek
Well i tried it out in VS2010 and it worked fine, however, i started a new solution in VS2008 and it did'nt. seems it might need a reset
Sir Psycho
+1  A: 

You need to access by the control's name, not by its class:

control1.DoSomething();

Generally speaking, UserControl classes are not visible in an ASP.Net project, since compilation adds the to different assemblies.

devio
A: 

Try doing things the other way around, can you access Ucrl:2 from Ucrl:1? Expose a public property and see if you can expose it:

from Ucrl:1 > Page Load >

Ucr2.MyProp

Sometimes doing a Build > Clean Solution then Build > Rebuild Solution and sort things out, or at least point you on the right track.

JamesM
I can't even access these controls from my aspx page. I thought by default all controls are in the ASP namespace but the controls do not appear there...Will try doing a clean thanks.
Sir Psycho