How do I reference the ID of a control that exists in the parent page of a user control?

Last post 07-06-2009 11:30 AM by Mark Barnes. 2 replies.

Sort Posts:

  • How do I reference the ID of a control that exists in the parent page of a user control?

    07-06-2009, 1:41 AM
    • Member
      16 point Member
    • Mark Barnes
    • Member since 12-29-2006, 2:57 PM
    • Posts 44

    I have a UserControl that contains contains a ModalPopupExtender.  I need to set TargetControlID to a control that will exist on the parent page of the UserControl.  Preferably I want this ID to exist as a property where the user can drop the control on the page and set the TargetControlID at design time.  For some reason, I am getting a null reference when I execute the code.  Here's a snippet of the code:


    public partial class MyUserControl: System.Web.UI.UserControl
    {

        private string _targetControlID;
        public string TargetControlID
        {
            get { return _targetControlID; }
            set { _targetControlID = value; }
        }

    ...

    }

    Here's the markup:

    <uc1:MyUserControl ID="MyUserControl1" runat="server"
              TargetControlID="OkButton" />


    I'm guessing the ID is out of scope and probably needs to be mangled to refer to the containment hierachy.  Any thoughts? Thanks in advance.



  • Re: How do I reference the ID of a control that exists in the parent page of a user control?

    07-06-2009, 6:21 AM
    Answer
    • Participant
      856 point Participant
    • Revdoniv
    • Member since 06-29-2009, 6:58 AM
    • Posts 168

    Set the id of the control with TargetControlId value in the page_Load of user control.

    if am wrong Please explain the error scenarion .


    Regards

    Revdoniv

    Please mark this as "Answer" if it helps you
  • Re: How do I reference the ID of a control that exists in the parent page of a user control?

    07-06-2009, 11:30 AM
    • Member
      16 point Member
    • Mark Barnes
    • Member since 12-29-2006, 2:57 PM
    • Posts 44

    Thanks a bunch.  Duh.  I forgot that the parent controls aren't instantiated yet when the accessor is called.  That's why I was getting a null reference exception.  For the benefit of all here's what to do:


     public partial class MyUserControl : System.Web.UI.UserControl
    {

        private string _targetControlID;
        public string TargetControlID
        {
            get { return _targetControlID; }
            set { _targetControlID = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            ModalPopupExtender1.TargetControlID = TargetControlID;
            ConfirmButtonExtender1.TargetControlID = TargetControlID;
        }
    }

Page 1 of 1 (3 items)