ASP.Net: Add additional code to Page_Load, on top of what is in codebehind?

If you can access the ASPX portion, you can try adding a code block to the ASPX, and overriding OnInit or OnPreInit, which should work fine for the code you're trying to add.

Fantastic, thank you very much for this suggestion. I had to use OnInit instead of OnPreInit, since the SetValue function gave an error of 'Non-static field requires a target' when I used OnPreInit. I guess it was too early in the page life-cycle to run this.

– philwilks Aug 31 at 7:22.

You would need to extract all source, add your code and rebuild the entire dll. There is not a way to do this without rebuilding the original assembly. This will be a problem if the original assembly is strong-named.

Otherwise, it's a pain but you should be ok.

1 Plus, if you're charged with maintaining a codebase it's helpful to actually have the source. – Mike C Aug 30 at 18:10 I don't think that's true. If he can access the ASPX, he can override OnInit or OnPreInit with an inline code block.

– James Johnson Aug 30 at 18:10 Ok, didn't think of that. I would consider this inadvisable and would prefer actually having and being able to change the source. Also, I don't know that OnPreInit is advisable as the control properties could be overwritten by ViewState.

– rtalbot Aug 30 at 18:30 Thanks for the suggestion, but I was really looking for a quick and dirty fix. We're going to be replacing the application in the next few months, and just needed to fix this in the short term. – philwilks Aug 300 at 7:23.

You can derive from the other Page class and add a Page. Load event handler: public class YourPage : TheirPage { public YourPage() { Load += YourPage_Load; } void YourPage_Load(object s, EventArgs e) { ... } } or even override OnLoad(): protected override void OnLoad(EventArgs e) { base. OnLoad(e); ... }.

– Mike C Aug 30 at 18:14 You put this class in your own assembly and instantiate it (e.g. , visit this page). The other code will be called because it's inherited. – Mark Cidade Aug 30 at 18:17 You would also need to change the ASPX to set the Inherits/CodeFile attributes to the new page class, no?

– rtalbot Aug 30 at 18:35 If there is an ASPX file available that's pointing to the other class, then yes, the Inherits property would have to be changed to the new Page class. – Mark Cidade Aug 30 at 18:42 Thanks Mark, this seams like the least hacky way of doing it. However I went with James' suggestion as it seemed a bit simpler.

– philwilks Aug 307 at 7:25.

Controls are loaded at this point and you should be able to do what you need. Sometimes additional processing is required and you can hook into LoadComplete as well which I think may serve you best? msdn.microsoft.com/en-us/library/system.... msdn.microsoft.com/en-us/library/system.....

If you can extract the code from the DLL by dissasembling it, you can disassociate the code behind the page and reimplement the logic in the markup.

You can derive from the existing class and override any of the rendering methods, such as the OnPreRender() method: public class Class1 : _Default { protected override void OnPreRenderComplete(EventArgs e) { base. OnPreRenderComplete(e); // add your code here } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions