User Control Within UserControl - Cannot access Parent User Control's Web Controls

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
dba123
8/14/2008 6:18:53 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

5 Replies
2487 Views

Similar Articles

[PageSpeed] 38

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
Lokira
8/14/2008 8:16:47 PM

It makes more sense if you look at inner first, I put them in the wrong order :)

0
Lokira
8/14/2008 8:39:44 PM

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
dba123
8/15/2008 1:10:02 AM

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
dba123
8/15/2008 12:55:46 PM

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
Lokira
8/15/2008 6:29:27 PM
Reply:

Similar Artilces:

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

Accessing Parent's control form User Control?
Hi, I have a Webform and a User Control in it. The form has a panel and textbox in it. The User control has a button in it. Using that Button, I want to show/hide the panel. But, my issue is how to get access to Parent's controls (in this case, the parent control is Panel). Please advice. Thanks in advance. Easy... just make the controls you want to access in the Page public and then cast the Page in the control to the appropriate type and access the now public controls.... uhh??? got that ;-) Example: Page: Name MyPage Controls in MyPage: public System.Web.UI.WebCo...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

Accessing Web Controls within a User Control
Hi, I've built a page which contains a user control. The contents of this user control is a dynamically-built series of radiobuttonlists. What I need to be able to do is access these radiobuttonlists from the code behind the outer page, the one containing the user control. However, it seems as though I cannot do this. Looping through control.controls I can see a placeholder control within my user control, but not the dynamically generated radiobuttonlists beneath it. As far as I'm aware you can usually access dyanamically generalted controls by using control.controls, but this seems di...

Access parent control in Web user control
Iam using Web User Control. I need to assign some values to the parent control. Is it possible to do it?. Thanks in advance. Cheers!!! Prashanthan KThanks! Prashanthan K I got the solution for this. Me.Parent.FindControl("") returns the parent control. Thanks Prashanthan KThanks! Prashanthan K...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

Persisting data in a web user control, which contains a list of other web user controls.
 Hi All, I have a control I've created, which can have a number of child controls - also user controls - stored as an ArrayList, which can be added wither a design time or programmatically.My problem is when I add to the list programmatically, it doesn't persist them. So if I add 3 child controls in step 1 of a wizard, with the aim that those three will be visible inside the parent in step 3, what happens is that the parent has no child controls and the item count is 0.Can anyone help? Thanks in advance.Code:Parent Control Markup: <%@ Control Language="C#" Auto...

Access Web Control's property within the Web Form
Hello, I have a Web Control which has a TextBox and a Button. I want to put this Control on a WebForm, then put some text in a Control's TextBox, press on Control's Button and be able to access text box's value from within the Web Form. That's my Control: _________________________________________________ public class Colo : System.Web.UI.UserControl { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.Button Button1; public string Myval{ get{return (string)ViewState["myval"];} set{ViewState["myval"] = va...

How to a call a function from a user control's parent user control ?
 hiHow to a call a function from a user control's parent user control ? for example i have one user control an tree in user control i add other user control in place holder for add node for tree Now how i can rebind tree when i add new node thank a lot  You can rebind the whole page if you do a Page.DataBind(). Alternatively, if you need to bind only one specific control in the parent, you can create an interface such as IMyParentUserControl, create a method definition within it such as:public interface IMyParentUserControl{     void DoMyBinding();} a...

Web user control inside update panel replace with another web user control
 Hi there. I have a page with a menu. an and then the content. My thing is. That i have the update panel, and inside that i have a regual panel in which i add the Web User Controls i need. Which all works fine. My problem is. That i i have a submit funcion on almoast all the Web User controls. What i would like is that i inside that update panel, can dispose the original Web user Control and then add a response Web User Control. In normal language. Is there a way that i can load one web user control into antoher and then disposose the original? Thanks in advance hello. well, what ...

Web resources about - User Control Within UserControl - Cannot access Parent User Control's Web Controls - asp.net.web-forms

Resources last updated: 1/17/2016 1:07:30 AM