User control in a user control

Hello all,

I have created a user control with some checkboxes, and public properties to get the values of these checkboxes. I have also created a second user control using the first user control; this second control also access the public properties of the first one.

My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem.

Thank you in advance for your help.

It is when you give from yourself that you truly give.

IT Images
8/9/2007 4:12:48 PM 91979 articles. 4 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 39
Get it on Google Play
Get it on Apple App Store


My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem.


By "wrong values" do you mean that the values of the inner User Control's properties revert back to their declaratively set or default values, rather than the values that have been programmatically set by the outer User Control?

Can you show us the code you use for the properties of the inner User Control? If my understanding of what you mean by "wrong values" is correct, then it may be that you are not backing the property values to viewstate, which will cause any programmatic property changes to be lost on postback.

Happy Programming!

-- Scott Mitchell
8/9/2007 4:23:35 PM

Thank you for your quick reply. 

here is part of the code in the inner control:



private Boolean _bEncryptConnectionString;

public Boolean bEncryptConnectionString
   get { return _bEncryptConnectionString; }
   set { _bEncryptConnectionString = value; }

protected void cbEncryptConnectionStrings_CheckedChanged(object sender, EventArgs e)
   bEncryptConnectionString = cbEncryptConnectionStrings.Checked;


You are right about reverting back to the default value. but I have already enabled viewstate to both controls inner and outer at design time, or is there another property that I should set?

 Thanks again,

It is when you give from yourself that you truly give.

IT Images
8/9/2007 4:40:28 PM

You have to manually save and retrieve the property value from the ViewState collection in your User Control property in order to have the value persisted across postback. Ditto for properties in custom, compiled controls. For common, scalar properties like bools, strings, ints, and so on, you can just stuff that value into the ViewState collection. For complex types (i.e., properties whose value is an instance of a custom class with a slew of properties) you need to employ view state serialization within that custom class itself.

The good news is that you have a simple example here. Just update the property to the following:


1    public Boolean bEncryptConnectionString
2    {
3       get { 
4          object b = ViewState["EncryptConnectionString"];
5          if (b == null)
6             return false; // return whatever you want the default value to be if this property isn't set explicity
7          else
8             return (bool) ViewState["EncryptConnectionString"];
9       }
10      set { ViewState["EncryptConnectionString"] = value; }
11   }


Note that there's no need for the private member variable  _bEncryptConnectionString anymore.

Happy Programming!

-- Scott Mitchell
8/9/2007 5:29:07 PM

Not only you have given me a clue but you also the solution :)

I wish you good luck in your life

Thank you

It is when you give from yourself that you truly give.

IT Images
8/9/2007 5:55:54 PM


Let me leave you with an article I would invite you to read for a more thorough understanding of view state in ASP.NET: Understanding ASP.NET View State. It was written for 1.x, but the majority of the concepts discussed are exactly the same in 2.0.

Happy Programming! 

Happy Programming!

-- Scott Mitchell
8/9/2007 6:08:58 PM

Similar Artilces:

User Control in a User Control
I have a first user control that contain a second user control. If I cahe the first user control can I still dynamically control the second one or it is cached with the first one therefore can not control it anymore? Thanks When your first control is cached, its rendered out and its output is stored in the cache. On every request to the page containing your control that can satisfy it from cache, the output will just simply be inserted - your first control will never be instantiated or run in that case, so the second one wont be either. Hope this helps,Mike VolodarskyProgram Manager...

user control to user control
I have an aspx page that have a "Register" directive to two usercontrol called rptMaster.ascx and rptDetail.ascx. I'm trying to set up a Master-Detail relationship and wish for the rptMaster.ascx to trigger and event from a dropdownlist and for the rptDetail to be listening for DropDownList event. I believe I have gotten the events part okay with help from this articel but my declaration for a private statement to my rptMaster class is producing an error. I'm using beta 2 of and I 'm having trouble getting a reference TO ...

User Controls in User Controls?
I have the follow set up and it is throwing an error "Unhandled error loading module" ... is there some trick to this that I don't know?I have a project A that uses controls from project B (project B is just a project with all my user controls used through out other modules).  project A uses user control 1 from project B with out any issues at all - great.  But then I wanted to add user control 2 to user control 1 and that is when I get my error.  In fact I don't even have the user control 2 in user control 1, just when I put the register part into user control 1 is when it al...

User Controls not working if I put Validation controls in user control
Hi           I have validation controls(RequiredField Validator,RegularExpressionValidator)  in user controls.User controls are not posting back like the events are not wiring up.If I remove Validation controls in User controls page,user controls are working fine.Can anyone please help me out in getting it fixed. In the Validation controls, make sure you are setting the ControlToValidate property to the control that it links to.  If you're in .NET 2.0, it's also a good idea to put all the validation controls in the same Valid...

How to access a user control from another user control in a Wizard control
My page has a wizard control and each step is a user control.  I could find the value of a control in step1 user control in the wizard page but not in step2 user control.From wizard.aspx.vb – it worksDim m_title As DropDownList = CType(Me.PersonalBox1.FindControl("ddlTitle1"), DropDownList) Question:How can I find the value of a control(textbox or dropdownlist)in step1 user control from step2 user control? I want to find “PersonalBox1.FindControl("ddlTitle1")” from Step2 AddressBox1. I have tried several ways to access it but no luck. From AddressBox.ascx.vb –...

User Control controls not created when dynamically creating user control
  Have created a user control for displaying and editing a dynamically number of values. The user control contains two labels and an edit box. When dynamically loading the user control I get an error when the control itself tries to change setting on the fields within the user control.   Why doesnt this work? It works okay if I dont load it dynamically but create a user-control-instance in the aspx-page but thats no solution.   Thankyou!     Here is my code:   ucDataValue.ascx   <%@ Control Language="C#" AutoEventWireup="true&...

Getting value of a property in Parent User control from a Child user control
I have a ChildUserControl that is loaded inside a ParentUserControl. The host page loads ParentUserControl.I would need to access properties in ParentUserControl from the ChildUserControl.Thanks for your time Here's some ideas: You can from the child access the parent and run findcontrol. child.parent.findcontroL("indyanguy1") child.parent.parent.findcontrol("indyanguy1") The trick is when you get your findcontrol working to cast the result to the object type your looking forHere I'm looking for a label so I cast to label then I can use the .Text property.Y...

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

Inherits user controls from my own user control.
Hi, I am working on ASP.NET 2.0 with the C# as code behind. I have a baseControl.ascx.cs which will have properties and some methods.Then I have childControl.ascx.cs which inherits from  the baseControl. Public partial class childControl : baseControl Both user controls are in the same namespace. When I build those user controls, it returns me error saying that the type or namespace baseControl cannot be found(). I would like to know is it possible to inherit user control? Will it be possible if I declared the baseControl as an abstract class since I want to have some abstract met...

Raiseevent user control in user control
 Hi there, I am working on a project in which a user control (BedrijfMasterDatagrid.ascx) is creating an other user control (BedrijfDatagrid.ascx).The user control BedrijfDatagrid.ascx is correctly created, but the clicked event of the created image control (BedrijfDatagrid.ibImageButtonClicked ) is not fired. (see BedrijfDetail.aspx)When I place BedrijfDatagrid.ascx directly on the page the event is correctly fired. Summary:BedrijfMasterDatagrid.ascx:       Datagrid with 1 row in which BedrijfDatagrid is dynamically createdBedrijfDatagrid.ascx  ...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2?   Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn You can define a property inside the UC1 and UC2. Pag...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2? Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn Hello, In UC2, try the followin code Dim tb As TextBox=CT...

user controls containing other user controls
I have a ContactInfo custom class that exposes AddressInfo custom class as property. I created two User Controls, you guess, Contact.ascx and Address.ascx. Address.ascx exposes property Address as AddressInfo. I am running into following problem. In Contact.ascx in page_load sub I am trying to set the values of the Contact class like this : _contact.PrimaryAddress = ctlAddress.PrimaryAddress but since value of the PrimaryAddress property in Address.ascx is set also in Page_Load, above code will not work since there is no instance of this object yet. Is my only option to mak...

user control inside a user control
i've currently got one of my pages setup so that I have a user control, CreateMessage.ascx, inside my main user control, ViewChangeMessages.ascx. My problem is that i've got a method inside my main usercontrol that either hides or shows the second user control, but it doesn't work, mainly coz i found out that the second user control is loaded before my main one. Is there any way i can get around this? You could try to load the second user control programmatically the OnPreRender method of your main user control. You'll need to add a placeholder in the spot you want the user control to...

Referencing User Control In Some Other User Control
hi, I have two user controls namely CanvasGrid.ascx and GridUtility.ascx. CanvasGrid.ascx is just a container for displaying GridView control in a placedholder GridPH. I want to get the checked rows from GridView control of CanvasGrid.ascx and do tagging against specific name selected from the TagDropDownList of GridUtility.ascx. But the problem is that I am not geting the reference of GridView as it is coming null everytime even when I am using FindControl() to find the GridView control on the page. GridUtility.ascx is a part of CanvasGrid.ascx and it is placed in a placehol...

Updating a Label control in one user control from another user control
Hi all, I have a main page in my music store site that has 5 user controls on it with 1 always showing main content and the others being things like a shopping cart, "best sellers", genre list etc (a la amazon). From the main content control I want to be able to set some label controls in my shopping basket control but I just can't seem to work out the syntax for how to do this: Here's the code I'm using - I'm trying to go to the 4th user control on the page (my shopping basket) then find the appropraite label, cast it locally as a Label then update the value: Label _so...

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

want to call one function in one user control from another user control
hi i have two user controls info.ascx and personal.ascx i have a function like in info.ascx i have a function like showdetails() {   } now i want to access this function from personal.ascx can i do this ? thanks in advance. shwetaThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path  you should better create a class where you will define the "functions" that you need in both files. declare "static" the function and call it like nameofclass.nameoffunction In a web project you can...

How to Load User Control From Within User Control
I'd like to load a user control, that has its code-behind compiled into an assembly, from within another user control. The user control you see below is used as a page template, contained within a .aspx file. There is no code in the MySite.Web.UI.Pages.PageTemplate class. <%@ Control Language="C#" Inherits="MySite.Web.UI.Pages.PageTemplate" %> <%@ Register TagPrefix="mysite" Namespace="MySite.Web.UI.Controls" Assembly="MySite.Web" %> <div id="container"> <div id="pageHeader"> ...

Accesing User Controls within User Control
I've a web page which is using a user control.  I need to put several user controls on that user control (which I'm loading on web page.).  My problem is how to access the user controls property on (which are on the user control) from web page. Please help. Thanks in advance. you need to create other property in your web control which will return an instance of your 2nd web controlDomain Names | ASP.NET 3.5 Hosting - $3.99/mo (10 GB Space, 300 GB Transfer, 200 MB MS SQL 2000/2005) Are you saying that you have multiple controls on one page that you want to talk to eachot...

Updating a User Control from another User control
I have a web page that has three frames: Header, Nav, and Content. In the header frame, I have a user control for the breadcrumb text (something like "Home : Cases : Jones Case : Contacts") that needs to be updated based on information sent from another user control which is in the Content frame. Is this possible?...

communication between user controls nested in a user control
Okay- trying to do this but not sure if I can . I want to build one user control that contains 1-10 user controls within. But how can I communicate between children user controls. Maybe I am a little lost without Intellisense. Anyway, if can provide me with a quick example in C# - I would appreciate it. Could I just reference controls individually by their "ID"s ? Thanks in advance.2 wrongs don't make a right - but three lefts do You could do something like this: foreach (Control c in Parent.Controls) { if (c is UserControl) { // com...

passing a parameter, user control to user control
There are two user controls on a Web form. What is the syntax to reference a propertiy on the other user control. The error message states: CS0246: The type or namespace name 'Results' could not be found (are you missing a using directive or an assembly reference?) Source Error: Line 7: { Line 8: //Set the department name property of the Results control Line 9: Results.DepartmentName = DepartmentList.SelectedItem.Text; Line 10: } Line 9 needs something different in order to reference this PROPERTY (DepartmentName) on the user control Results....

User control with Property to connect to other user control
Hi,I'm a bit a newb to ASP.NET ;)I want to create a user control which can be connected to another user control. So I did the following:   Private IntBreadcrumbsControl As MenuControl <Category("CMS"), Description("Set the MenuControl that displays the breadcrumbs"), Browsable(True)> _ Public Property BreadcrumbsControl() As MenuControl Get Return IntBreadcrumbsControl End Get Set(ByVal value As MenuControl) IntBreadcrumbsControl = value End Set End Property Unfortunat...

Web resources about - User control in a user control -

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Paris attacks: ‘Did your French gun control stop a single f-ing person from dying at the Bataclan?’
Band's frontman supports the National Rifle Association and is a gun enthusiast. Such views seem incongruous in the city of love.

WA fires: Harvey bushfire threatening Brunswick contained and under control
A bushfire that threatened parts of Brunswick has been further downgraded to "advice" as the threat to lives and homes receded.

Use your iPhone to control any AA-powered device with this simple $10 adapter
HomeKit -certified devices are the slickest way to control your technology, but if you have any dumb technology powered by AA batteries, you ...

Inside the wild—and wildly successful—early years of mission control
Mission Control, on the third day of Apollo 8. Seen on the TV monitor is a picture of Earth telecast from the spacecraft 176,000 miles away. ...

Your iPhone can control anything you own that’s powered by AA batteries
Looking for a brilliantly simple and simply brilliant solution to control anything you own that's powered by AA batteries with your iPhone? From ...

Eagles of Death Metal playing Paris tonight; Jesse Hughes speaks out against gun control
Eagles of Death Metal are returning to Paris tonight (2/16) to "complete" their show at Le Bataclan that was a site of the Paris terror attacks. ...

Kids Today Can Whip Around In Mini Electric Cars While Their Parents Control The Action From An App
... my eye as a girl whipped it around a mini track at the convention center, a big ol’ grin on her face. My 8 y.o self is jealous: Parents control ...

Pope Francis signals openness to birth control for Zika virus
Francis also said St. John Paul II's friendship with a married woman was nothing more than that.

How to control your Mac’s volume, app by app
... while the next is too high? Well, shocker! There’s an app for that. It’s called Volume Mixer. This simple app supercharges your volume control ...

Resources last updated: 2/19/2016 12:29:17 PM