Take the 2-minute tour ×

I have a parent ascx control which has a hidden field and some public properties defined. It also has a placeholder for some dynamic child controls to be inserted at run time. All the child controls inherit from the parent control, so these public properties are available to them. Here is the ascx code for the parent;

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyAccount.ascx.cs" Inherits="MyAccount.UI.MyAccount" %>

<asp:hiddenField id="hidAcctID" value="-1" runat="server" />
<div id="myaccountcontainer">
  <asp:PlaceHolder ID="phdContent"  runat="server"></asp:PlaceHolder>

Here is the parent's code behind

 partial class MyAccount : PortalModuleBase
        #region Public Properties
        public string SharedResourcesFile 
                return ControlPath + "App_LocalResources/SharedResources.resx"; 

        public int AccountId
                int id;
                Int32.TryParse(this.hidAcctID.Value, out id);
                return id > 0 ? id : -1;
                hidAcctID.Value = value.ToString();

The 1st problem I'm having is when I try to access the AccountId property from a child control, the code is looking for the hidden field in the child control instead of in the parent control. One of my solution to this is do this on every child;

 var hidfld = (HiddenField)this.Parent.FindControl("hidAcctID");
 int id = Convert.ToInt32(hidfld.Value);

This works fine but it's a lot of duplicate code

The 2nd Problem: The parent control and the resourcefile are located in



while the child controls are located in


When I access the SharedResourcesFile property(which returns the location of the resource file) from the child controls, the path returned also includes the ChildControls folder which is not where the resourcefile is. Again I can remedy this by putting the child controls in the same folder as the parent.

Is there a way to solve the above problems? Thanks

share|improve this question
Access the file without the ControlPath. by "~DesktopModules/ModuleName/App_LocalResources/SharedResources.resx". If all the children descend of the same type, you could assign the hidden field value when you create them. Otherwise it seems that you may have to 'go back to the blackboard' with your object design. –  istepaniuk Jan 13 '13 at 22:03
@istepaniuk, that solves problem No.2(the path of the resource file). The only downside is the hard coding of the module location which means if the module is relocated, the code breaks. Could you elaborate how you would assign the value of the hidden field to the child control at creation? Thanks –  Moses Machua Jan 14 '13 at 0:46
@istepaniuk Correction: "~/DesktopModules/ModuleName/App_LocalResources/SharedResources.resx". You have to include the forward slash before DesktopModules –  Moses Machua Jan 14 '13 at 1:14

Your Answer



Browse other questions tagged or ask your own question.