Custom User Control & Modal Popup Extender RRS feed

  • Question

  • User-2127570633 posted

    Hi, I'm creating a custom user control for asp.net wich is embedding a ModalPopupExtender. The modal popup extender is added to the controls list in the ShowPopup method handling the imageclick event. The problem is that when another postback is fired i can't access the modalpopupextender to call methods such as the close method (that is what i'm going to do), i've tried to lookup the control from the controls collection of the UserControl via the FindControl method but i can't find it ! Below is the code... please help me !

    public abstract class AbstractFieldUserControl : System.Web.UI.UserControl
            public AbstractFieldUserControl()
                this.Controls.Add(new LiteralControl() { ID = "LI_Code"} );
                this.Controls.Add(new LiteralControl(" - ") { ID = "LI_Sep"});
                this.Controls.Add(new LiteralControl() { ID = "LI_Description"});
                ImageButton img = new ImageButton();
                img.ID = "IM_Open";
                img.ImageUrl = "~/img/edit.png";
                img.ImageAlign = ImageAlign.AbsMiddle;
                img.Click += new ImageClickEventHandler(this.ShowPopup);
            private void ShowPopup(object sender, ImageClickEventArgs e)
                if (this.PopupContent == null)
                    throw new NullReferenceException("PopupContent propery cannot be null");
                Button b = new Button();
                b.ID = "BT_Target";
                b.Style.Add("display", "none");
                ImageButton imOpen = (ImageButton)FindControl("IM_Open");
                ModalPopupExtender popup = new ModalPopupExtender();
                popup.TargetControlID = b.ID;
                popup.OkControlID = imOpen.ID;
                popup.ID = "Popup";
                popup.PopupControlID = this.PopupContent.ClientID;
            public Control PopupContent { set; get; }
            public abstract void InitPopupContent();
            protected void SetCodeDescription(object sender, EventArgs e)
                string[] args = ((Button)sender).CommandArgument.ToString().Split(';');
                (FindControl("LI_Code") as LiteralControl).Text = args[0];
                (FindControl("LI_Description") as LiteralControl).Text = args[1];
            protected void SetCodeDescription(string code,string description)
                (FindControl("LI_Code") as LiteralControl).Text = code;
                (FindControl("LI_Description") as LiteralControl).Text = description;
            public string Code {
                set { (FindControl("LI_Code") as LiteralControl).Text = value; }
                get { return (FindControl("LI_Code") as LiteralControl).Text; }
            public string Description
                set { (FindControl("LI_Description") as LiteralControl).Text = value; }
                get { return (FindControl("LI_Description") as LiteralControl).Text; }


    Wednesday, September 2, 2009 3:07 PM


  • User-2106054853 posted


    If you want to add controls dynamically, please add it in OnInit event handler. If you don't want to render it, please set Visible property of the control to false. If you want to "add" it in a postback event handler, please set Visible property of the control to true in that event handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 3, 2009 10:01 PM