954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic control event

i know this partially asp.net as well...


if i create an custom control like this

protected void Page_Load(object sender, EventArgs e)
{
Control myUserControl = (Control)LoadControl("MyControl.ascx");
PanelX.Controls.Add(myUserControl);

is there a way of responding to an event within that control...

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("WebUserControl1 :: I am in the control");
}

without specifically creating an instance of the control...

MyControl realControl = new MyControl();
realControl.myEvent += new EventHandler(FormEvent);

Where FormEvent is an event on the form triggered by the event on the control.

hope this makes sense.

pw_jamison
Newbie Poster
4 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, you can attach the event handler yourself after you have added the control:

protected void Page_Load(object sender, EventArgs e)
{
    Control myUserControl = (Control)LoadControl("MyControl.ascx");
    PanelX.Controls.Add(myUserControl);
    myUserControl.SomeEvent += new SomeEventHandler(eventHandler_Method);
}

private void eventHandler_Method(object sender, EventArgs e)
{
   //your code here
}


I have a little experience with ASP.NET but if anyone more experienced wants to chime in and correct me, go ahead, but i believe you should move the control creation to the page Init event rather than the page load. If you have a look at the page lifecycle , the hardcoded controls are added during Initialization and then any values in viewstate are set during Load. By adding them in the Init event you can be sure they are present if any code tries to access them OnLoad :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

thanks Ryshad,
I've tried doing that, but because the control is a 'Control' type it doesn't pick up the 'SomeEvent' event of the user control.
I don't want to hard code cast the control as it's actual type as this kind of defeats the purpose of dynamically creating it.

Control myUserControl = (Control)LoadControl("MyControl.ascx");
PanelX.Controls.Add((MyControl)myUserControl);
probably doesn't technically work, but conceptually this is what i DON'T want to do.

unless i can do something like...

Type thisType = MyControl.GetType();
PanelX.Controls.Add((thisType)myUserControl);

...but then that probably wouldn't expose the contained event either.

i was reading about event bubbling, but all the examples i've seen whilst user controls are all hard coded.

ta,
paul

Yes, you can attach the event handler yourself after you have added the control:

protected void Page_Load(object sender, EventArgs e)
{
    Control myUserControl = (Control)LoadControl("MyControl.ascx");
    PanelX.Controls.Add(myUserControl);
    myUserControl.SomeEvent += new SomeEventHandler(eventHandler_Method);
}

private void eventHandler_Method(object sender, EventArgs e)
{
   //your code here
}
pw_jamison
Newbie Poster
4 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

ok i found the solution eventually in a seemingly unrelated topic...

the child control uses this...

private void Button1_Click(object sender, System.EventArgs e)
{
  CommandEventArgs ee = new CommandEventArgs("ButtonClicked", null);
  RaiseBubbleEvent(this, ee);
}


the parent control has one of these to handle it...

override protected bool OnBubbleEvent(object sender, System.EventArgs e)
{
  bool isHandled = false;

  if (e is CommandEventArgs)
  {
    CommandEventArgs ee = (CommandEventArgs)e;

    switch (ee.CommandName)
    {
      case "ButtonClicked":

        DynamicBaseCtrl thisD = (DynamicBaseCtrl)sender;
        Dictionary<string, string> thisData = new Dictionary<string, string>();
        thisData = thisD.GetData();
        lblMultiControl.Text = thisData["Text"].ToString();

        localData = thisData;

        isHandled = true;
        break;
      }
    }

    CommandEventArgs ez = new CommandEventArgs("ButtonClicked", null);
    RaiseBubbleEvent(this, ez);

    return isHandled;
  }


...which in this instance triggers another RaiseBubbleEvent which is passed to the aspx page which has it's own OnBubbleEvent waiting for it.

I'm sure there is a more official/proper way but this got me the result i was after.

pw_jamison
Newbie Poster
4 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: