late binding an event to an existing event handler

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
I need some help finishing-off what I am trying to do, which is late bind to an existing event handler:

 EventInfo eInfo = controlType.GetEvent(myEvents.ElementAt(j).Name);
                    Type handlerType = eInfo.EventHandlerType;
                    MethodInfo invokeMethod = handlerType.GetMethod("create_new_user_NameAvailabilityEvent");
                    Delegate d = Delegate.CreateDelegate(handlerType, invokeMethod);
                    eInfo.AddEventHandler(c,d);

Open in new window



Right now

MethodInfo invokeMethod = handlerType.GetMethod("create_new_user_NameAvailabilityEvent");

is returning null, so I'm doing something wrong there.

Here is the enitire code:


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

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)
            {
                GlobalMethods.InitControlList();
            }

            string layout = "";

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

            DisplayLayout(layout);
        }

        private void DisplayLayout(string layout)
        {
            GlobalMethods.divlayoutgencontrols.Clear();

            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");

                Type controlType = c.GetType();
                BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
                EventInfo[] myEvents = controlType.GetEvents(myBindingFlags);

                for(int j = 0; j < myEvents.Count(); j++)
                {
                    EventInfo eInfo = controlType.GetEvent(myEvents.ElementAt(j).Name);
                    Type handlerType = eInfo.EventHandlerType;
                    MethodInfo invokeMethod = handlerType.GetMethod("create_new_user_NameAvailabilityEvent");
                    Delegate d = Delegate.CreateDelegate(handlerType, invokeMethod);
                    eInfo.AddEventHandler(c,d);
                    ParameterInfo[] parms = invokeMethod.GetParameters();                    
                }              

                c.Visible = true;
                tempGUID = Guid.NewGuid().ToString();
                c.ID = settingsarray[0] + tempGUID + i.ToString();

                GlobalMethods.divlayoutgencontrols.Add(c);
            }

            foreach (Control c in GlobalMethods.divlayoutgencontrols)
            {
                sitemanagercontrolsdiv.Controls.Add(c);
            }


            Master.CalcCartTotal();
            Master.ShowCat();
            Master.DisplayUser();

        }





/////////////////  I WANT TO LATE BIND TO THIS EVENT HANDLER ///////////////////////////////

        void create_new_user_NameAvailabilityEvent(bool wasavailable)
        {
       //     create_new_user.ShowRotatingArrow(true);

            if (wasavailable)
            {
                //LabelResultMessage.Text = "That username is available.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;
            }
            else
            {
                //LabelResultMessage.Text = "That username is already taken.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;

            }

          //  create_new_user.ShowRotatingArrow(false);
        }
    }
}

Open in new window


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Tom KnowltonWeb developer

Author

Commented:
Can you spell it out for me?  As in...provide working code?

Here is my revised code:


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

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)
            {
                GlobalMethods.InitControlList();


                string layout = "";

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

                DisplayLayout(layout);

            }
           
        }

        private void DisplayLayout(string layout)
        {
            GlobalMethods.divlayoutgencontrols.Clear();

            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");

                Type controlType = c.GetType();
                BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
                EventInfo[] myEvents = controlType.GetEvents(myBindingFlags);

                for(int j = 0; j < myEvents.Count(); j++)
                {
                    string eventName = myEvents.ElementAt(j).Name;
                    if (eventName == "NameAvailabilityEvent")
                    {
                        Delegate d = Delegate.CreateDelegate(myEvents.ElementAt(j).EventHandlerType, this,myEvents.ElementAt(j).Name);
                        c.GetType().GetEvent(eventName).AddEventHandler(c, d);
                    }
                }              

                c.Visible = true;
                tempGUID = Guid.NewGuid().ToString();
                c.ID = settingsarray[0] + tempGUID + i.ToString();

                GlobalMethods.divlayoutgencontrols.Add(c);
            }

            foreach (Control c in GlobalMethods.divlayoutgencontrols)
            {
                sitemanagercontrolsdiv.Controls.Add(c);
            }


            Master.CalcCartTotal();
            Master.ShowCat();
            Master.DisplayUser();

        }

        public static void NameAvailabilityEvent(bool wasavailable)
        {
       //     create_new_user.ShowRotatingArrow(true);

            if (wasavailable)
            {
                //LabelResultMessage.Text = "That username is available.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;
            }
            else
            {
                //LabelResultMessage.Text = "That username is already taken.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;

            }

          //  create_new_user.ShowRotatingArrow(false);
        }
    }
}

Open in new window

Tom KnowltonWeb developer

Author

Commented:
ANOTHER REVISION:


This code compiles and runs okay........but the event does not fire.


Why doesn't the event fire?




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

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)
            {
                GlobalMethods.InitControlList();


                string layout = "";

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

                DisplayLayout(layout);

            }
           
        }

        private void DisplayLayout(string layout)
        {
            GlobalMethods.divlayoutgencontrols.Clear();

            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");

                Type controlType = c.GetType();
                BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
                EventInfo[] myEvents = controlType.GetEvents(myBindingFlags);

                for(int j = 0; j < myEvents.Count(); j++)
                {
                    string eventName = myEvents.ElementAt(j).Name;
                    if (eventName == "NameAvailabilityEvent")
                    {
                        //Delegate d = Delegate.CreateDelegate(myEvents.ElementAt(j).EventHandlerType, this,myEvents.ElementAt(j).Name);
                        //c.GetType().GetEvent(eventName).AddEventHandler(c, d);






                        //btn is "c"

                        EventInfo evt = c.GetType().GetEvent(myEvents.ElementAt(j).Name);
                        MethodInfo handler = GetType().GetMethod("SomeHandler");
                        evt.AddEventHandler(c, Delegate.CreateDelegate(
                                evt.EventHandlerType, this, handler));











                    }
                }              

                c.Visible = true;
                tempGUID = Guid.NewGuid().ToString();
                c.ID = settingsarray[0] + tempGUID + i.ToString();

                GlobalMethods.divlayoutgencontrols.Add(c);
            }

            foreach (Control c in GlobalMethods.divlayoutgencontrols)
            {
                sitemanagercontrolsdiv.Controls.Add(c);
            }


            Master.CalcCartTotal();
            Master.ShowCat();
            Master.DisplayUser();

        }

        public void SomeHandler(bool wasavailable)
        {
       //     create_new_user.ShowRotatingArrow(true);

            if (wasavailable)
            {
                //LabelResultMessage.Text = "That username is available.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;
            }
            else
            {
                //LabelResultMessage.Text = "That username is already taken.";
                //ButtonClosePanelMessage.Visible = true;
                //PanelMessage.Visible = true;

            }

          //  create_new_user.ShowRotatingArrow(false);
        }
    }
}

Open in new window

Tom KnowltonWeb developer

Author

Commented:
thx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial