[2005] Page Control Events
I have a class that inherits from Page.
In the OnLoad method i'm looping through all controls on the page (recursively) before i call base.OnLoad. If the control is a ImageButton (in this example) then i want to add an event handler for its clicked event.
Code:
if (Ctrl is ImageButton)
{
((ImageButton)Ctrl).Click += new ImageClickEventHandler(ImageButtonClick);
}
This code definatly gets called for each control that is an ImageButton.
My ImageButtonClick event is as below:
Code:
public void ImageButtonClick(object sender, ImageClickEventArgs e)
{
new GUIWebEvent(sender.ToString(), "Click").Raise();
}
This NEVER gets called. I've double checked this by inserting int x = 0;x = 1 / x; within the method, so i know it never gets called.
Any ideas why? My only thought is that i'm doing it in the OnLoad method, but i'm pretty stuck as to how i can get this to work.
Re: [2005] Page Control Events
According to the MSDN the ASP.Net page lifecycle states that OnInit is called before OnLoad. OnInit is where all controls are created / initialized and the ViewState / ControlState is loaded so I see no issues with you using the OnLoad event.
Looking at the ImageButton Members, perhaps you need to specify it as OnClick rather than Click?
Re: [2005] Page Control Events
I don't think it'd be OnClick, since that's the method that's called when the event is fired. I want to add my handler method to the actual Click event its self (just like you'd do on a windows.forms application).
Re: [2005] Page Control Events
Quote:
Originally Posted by SLH
I don't think it'd be OnClick, since that's the method that's called when the event is fired. I want to add my handler method to the actual Click event its self (just like you'd do on a windows.forms application).
I understand what you're trying to do and I don't really use many ASP controls but according to the OnClick property in the MSDN, the example specifically assigned the event method to call to the OnClick property so it seems odd that you're using Click instead of OnClick.
Re: [2005] Page Control Events
That's a protected method and so i can't access it in the code-behind (without overriding ImageButton).
I think the example works because it's doing it on the actual ASPX page.
Looking at the MSDN page for the click event it has the declarations for c#, vb.net ... and asp.net, where for asp.net it has the line OnClick=...., so i guess the OnClick reference in the ASPX file is the same.
There must be something daft i'm doing though, since all the examples i've seen appear to be the same as mine (control.Click += .....)
Re: [2005] Page Control Events
This simple base class works fine for me, which is really nothing different than what you seem to be doing. So using OnLoad is fine...
Code:
public class PageBase : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
System.Diagnostics.Debug.Print("OnLoad");
foreach (Control ctrl in Page.Form.Controls)
{
if (ctrl is ImageButton)
{
((ImageButton)ctrl).Click += new ImageClickEventHandler(ImageButtonClick);
}
}
base.OnLoad(e);
}
protected void ImageButtonClick(object sender, ImageClickEventArgs e)
{
System.Diagnostics.Debug.Print("ImageButtonClick");
}
}
Re: [2005] Page Control Events
I think my issue is that the imagebutton's are in an UpdatePanel (an AJAX web control that automatically performs async postbacks for child controls).
Not sure how to resolve this issue though.
Re: [2005] Page Control Events
I've managed get most controls to work by having the class replace (and inherit from) ScriptManager, rather than Page.
One control that doesn't work still is the Menu control. Not sure quite why though but i can live with it.