Reflecting over the Page_Load method returns null
My question is related to retrieving attributes over a method which is called as part of a delegate _[e.g. Page.OnLoad or a Button_Click]_
I have a method attribute [MyMethodAttribute(PropertyOne, PropertyTwo)] over the _[Page_Load]_ or _[AnyWebControl_Event]_. This method attribute needs to be queried at runtime over the method on which it was placed. The method resides in the code-behind of any web page. All web pages derive from BaseWebPage. The event can be either a page load or a post back event. Based on whether the event was triggered by Page Load or a postback, I get a handle to the control [a page, or the postback] and add my method [MyMethod] to be executed.
_eventControl = GetPostBackControl(page); //__EVENTTARGET or any control
if (_eventControl != null) // this is a postback control for any page
{
_eventControl.Load += new EventHandler(MyMe开发者_Python百科thod);
}
else // this is PageLoad method for any page
{
_eventControl = page;
page.Load += new EventHandler(MyMethod);
}
After which, I am trying to find the event [e.g. Page_Load] which triggered the page load. This is where I am not able to get a handle to the method and tried all different ways around it.
One of the ways was to query which event delegate triggered the load or postback. But the event cannot be accessed from outside of the class, so I am not able to use MyDelegate.GetInvocationList() as this returns null.
Any help is appreciated.
After some more effort, I have found that __EVENTARGUMENT will provide me the method name. I have a different issue now. When I reflect over the ASPX page for the Page_Load method, it returns null. Any ideas?
Page_load is a protected method and I was using Public BindingFlags. So after using NonPublic | Instance flag I am able to get to the Page_Load method with relfection
After some more effort, I have found that __EVENTARGUMENT will provide me the method name.
Page_load is a protected method and I was using Public BindingFlags. So after using NonPublic | Instance flag I am able to get to the Page_Load method with relfection
What about Page.IsPostback? If IsPostback is false then the the page load was not triggered by a postback and is a fresh load.
精彩评论