troubleshooting Question

discover control events?

Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America asked on
5 Comments1 Solution247 ViewsLast Modified:
At runtime, is it possible to discover programmatically which events are exposed by a user control (.ascx) and subscribe to them in some generic fashion?

What I am wanting to accomplish is to read in settings from a "layout" file which tells me which .ascx controls to load into memory (and display on the form).  I can get that to work, but then I don't know how to subscribe to the events of these controls, since I am dealing with them as a collection of generic "controls" vs a collection of different control types.

So the process would be something like this:

-read in the file
-run "LoadControl" for each control name listed in the file
-subscribe to any public events the control emits
-provide some sort of central processing for the events (yet to be determined).

Is this making any sense?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Campus_Webstore
    public partial class SiteManager : System.Web.UI.Page
        protected void Page_PreInit(object sender, System.EventArgs e)
            this.MasterPageFile = "~/contentfiles/" + Page.StyleSheetTheme + "/TCSSite.Master";

        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)

            string layout = "";

            if (Request.QueryString["layout"] != null)
                layout = Request.QueryString["layout"] as string;
                layout = "default";


        private void DisplayLayout(string layout)

            StreamReader sr = new StreamReader(Server.MapPath("~/CustomLayouts/") + Page.StyleSheetTheme + "/" + layout + ".xml");
            string filecontents = sr.ReadToEnd();
            string countstr = GlobalMethods.ExtractValueFromXML(filecontents, "<count>");
            int count = Convert.ToInt32(countstr);
            string tempGUID = "";
            string settings = "";
            string[] settingsarray = new string[count];

            for (int i = 1; i <= count; i++)
                settings = GlobalMethods.ExtractValueFromXML(filecontents, "<" + i.ToString() + ">");
                settingsarray = settings.Split(',');

                Control c = LoadControl("~/UserControls/" + settingsarray[0] + ".ascx");
                c.Visible = true;
                tempGUID = Guid.NewGuid().ToString();
                c.ID = settingsarray[0] + tempGUID + i.ToString();


            foreach (Control c in GlobalMethods.divlayoutgencontrols)



the layout file:

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros