If I have a user control A and then inside it a user control B embedded/referenced within it. User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.
So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:
cpeNewUser.Collapsed = true;
So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time? How would I get this to work becuase I'm using all user controls inside a AJAX Toolkit Tab Control's panel.
When is Microsoft going to get rid of VB.NET!
![]() |
1 |
![]() |
One way to deal with this is by creating an event on the inner user control that the outer user control then picks up and actions:
Outer ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="outer.ascx.cs" Inherits="q1.outer" %>
<%@ Register src="~/inner.ascx" TagName="inneruc" TagPrefix="uc" %> <asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:Label ID="Label1" runat="server" Text="HELLO!"></asp:Label>
</asp:Panel>
<uc:inneruc ID="ucInner" runat="server" />
Outer ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace q1
{
public partial class outer : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ucInner.TogglePanelOnOuter+=new EventHandler(ucInner_TogglePanelOnOuter);
}
void ucInner_TogglePanelOnOuter(object sender, EventArgs e)
{
Panel1.Visible = !Panel1.Visible;
}
}
}Inner ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="inner.ascx.cs" Inherits="q1.inner" %> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />Inner ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace q1
{
public partial class inner : System.Web.UI.UserControl
{
public event EventHandler TogglePanelOnOuter;
public void Button1_Click(object sender, EventArgs e)
{
TogglePanelOnOuter(sender, e);
}
}
}Hope that helps you along.
![]() |
1 |
![]() |
It makes more sense if you look at inner first, I put them in the wrong order :)
![]() |
0 |
![]() |
Thanks, but I am looking for the opposite. I want the inner control to be able to pick up events off the outer user control's web controls. I mean is this the only way to do this, by having to do all this custom event code to just communicate back and fourth? It would take me a month to really understand how to code and maintain soemthing like this easily.
When is Microsoft going to get rid of VB.NET!
![]() |
0 |
![]() |
I'm just really confused on this thing. I've been told all you need to do is expose the web controls in your parent .ascx as public properties. Then somehow any child controls (.ascx pages) in the parent would see that public property(s). So I added a property to the code behind of my parent .ascx. Recompiled my website. Then I tried in the code-behind of my child to see if I could start typing that property and use it. Intellisense did not pick it up.
Then there's the solution I have seen out there that says do a recursive find and all this crap through the stack to find your public control or bubble up events. I think that's different than me just being able to access the web controls inside my parent from the code-behind of my child .ascx. I"ve seen this on the net. But I do not get this either. What this says to me is you can code all this stuff in design time but at runtime it tries to go out and find the ID? That ID would be different anyway because the INamingContainer wuold give it a random ID as you probably know, or at least an ID that's not the same as what you see in code-behind.
My goal is to be able to use intellisense just like if you had a referenced .ascx from an .aspx in code behind. I want to be able to do my code in my child .ascx, have it know about the contol that exists in my parent .ascx and be able to stub the code out before I compile because I'm getting an error as I stated that the code behind in my child has no clue about the web control ni my parent which is the original probelm in design time. So I figured adding the public property would work but it hasn't...or I'm doing it wrong.
When is Microsoft going to get rid of VB.NET!
![]() |
0 |
![]() |
It's probably worth mentioning that this has likely not been made too easy to do as it creates a dependancy from your child control to the parent, which can become difficult to manage.
Will the user controls in question always be nested together? If yes then maybe they should be combined into a single user control, if not then you're going to get issues when the child user control goes looking for controls on some different parent.
Some client side code (emitted at end of page) to find and manipulate the control in question might work, wouldn't reccomend it though.
![]() |
0 |
![]() |