Razor
Razor

Reputation: 17518

What can't I access a UserControl from my aspx.cs file?

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
    }

Upvotes: 1

Views: 1375

Answers (3)

JamesM
JamesM

Reputation: 1038

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.

Upvotes: 0

devio
devio

Reputation: 37225

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.

Upvotes: 1

this. __curious_geek
this. __curious_geek

Reputation: 43217

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.

Upvotes: 0

Related Questions