Where is the Component Initialize Method in Asp.NET 3.5 - c#

I want to create my own naming convention for page events rather than AutoEventWireUp but I couldn't find Component Initialize methods any where ? Should I override it ? But in which class it is defined ?
Thanks...
Edit :
For example : I don't want to use Page_Load but LoadThisPage naming. So It should be like
Load += new LoadThisPage(sender,e);
I was expecting a InitializeComponent method where I can initialize page,controls etc. events handlers...But it turned out to be Constructor function :)
So what confused me is I thought there should have been a method like InitializeComponent which does things for me already created by Designer itself so I thought I could define my own event handler names within this method by overriding it in the say Default.aspx.cs .
But the answer was simple :) Thanks...

Your question isn't really clear as to what you're trying to do.
Page events are defined in the System.Web.UI.Page class, some of which are inherited from System.Web.UI.Control. You don't need to use AutoEventWireUp if you don't want to, and you're free to override all of the Page methods that would normally raise the lifecycle events (OnInit, OnLoad, OnPreRender, etc.) and then not call the base methods, effectively squelching the events from being raised.
You can see some limited discussion around this on this blog post.
As Hogan noted, this sounds like a bad idea. Could you expand on what you're trying to accomplish?

This sounds like a bad idea to me.
However, you should be able to find auto-created code by selecting the project display options, I believe it is right click in project explorer, and selecting display hidden files or display all files. Then you will see additional .vb files created by the system. You might also have click on the little plus sign.
additonal added notes
Most of the events (btw) are defined in the base class(es) in System.Web.UI.Page and not in the code created for a specific instance.

Related

Which Button called this form?

I have 2 problems:
"Which Button called this form?" (short version)
I can not touch the button or button's form
Why do I want this?
I have many forms and need to know how the user got there. If I could get the Form (not the button) it may also solve the problem.
Long version: I need to copy some properties of source form/button to the new one without do it manually and I intend to use it later on Exception reporting to catch more info
Initially, i tried to do a "newForm.Caller = this;" on each button but there are 200+ forms and lots of buttons on each.
All forms and it's buttons are custom controls so I can do things there.
Tried things
I tried to do things with StackFrames and reflection at form constructor but don't work (889310)
I found this 10401190 for JAVA but it can't help
I thought I could use the OnClick override to store the last button instance in a static place in buttons/forms class then get it in the form constructor but seems to be the worst solution. (Many things open forms and the culprit would be the last button pressed)
The problem get worse when other things open Forms and I lost the reference (DataGridVewButton, timers, linked label, ...)
EDIT1: (oɔɯǝɹ)
Another detail, forms can be called from external Plugins. So again I don't have acesses to the code to change it.
EDIT2: Example (Graham Bass,ShreyasKapur)
FormA has a ButtonA that when clicked shows FormB
FormA inherits FormBase
Button inherits ButtonBase
FormB inherits FormBase
I can NOT change FormA neither ButtonA codes, only FormBase and ButtonBase codes
Edit3: (Bradley Uffner)
ShowDialog() forms have the Owner property that solves part of the problem. Thanks Bradley, I forgot about that!
Unfortunately, all existing code uses the parameterless constructor.
"Displays this form as a modal dialog box with no owner window" (1)
I would think that you are trying to solve the wrong problem.
When your forms are this interconnected, you coupled them to tightly. By coupling them even more tightly by looking back to who called you, you are only making you problem worse. See also: the comefrom instruction.
I would suggest passing parameters between your forms to supply them the data they need. But keep the number of parameters to an absolute minimum, and don't try to use something like caller, that would be cheating.

Is there an event which fires when the Layout Details screen is opened?

I'd like to write some code that fires when the layout details screen is opened. Is there an event, some sort of API or some way to hijack Sitecore UI to allow me to do this? There's nothing in the Sitecore.config events sectiont hat looks relevant.
What I want to do is capture the ID of the item for which I'm currently updating so that I can use it in custom controls later on.
I'm using Sitecore 6.6.
Marek Musielak's answer is great. An alternative would be to change the command fired from the 'Details' Button - Look for item:setlayoutdetails in the commands.config.
You could create your own class that does your work then instantiates Sitecore.Shell.Framework.Commands.SetLayoutDetails afterwards.
You can override the \sitecore\shell\Applications\Content Manager\Dialogs\LayoutDetails\LayoutDetails.xml and change its code beside:
<CodeBeside
Type="Sitecore.Shell.Applications.ContentManager.Dialogs.LayoutDetails.LayoutDetailsForm,Sitecore.Client"/>
to inherit from your custom class that will simply inherits from LayoutDetailsForm and overrides e.g. OnLoad method.

how to call a event from form1 in form?

I need to call tabControl1_SelectedIndexChanged from Form1 in Form2
I have no idea how do this.
In general, you don't call events from other classes. The idea is that events expose subscribe/unsubscribe behaviour. The implementation can choose to also expose a method which raises the event, but it doesn't have to - and if the control you're using doesn't expose such a method for the SelectedIndexChanged event, you can't force it to.
It's not clear what you're trying to achieve, but you may be able to programmatically select the appropriate tab instead - I'd expect that to raise the appropriate event. Rather than expose the tab control directly from Form1 to Form2 (which I hope are only placeholder names - give your forms meaningful names :) it would be cleaner to expose a method in Form1 to perform the selection of the appropriate tab. That's a more meaningful operation to perform on Form1 - it doesn't rely as heavily on the implementation details. On the other hand, you may be able to create an even cleaner design using MVP patterns (or whatever suits you best).
You should make a public method in the first form that performs the logic you need.
Then, pass an instance of the first form to the second form and call the method on that instance.
As Jon mentioned, you shouldn't make public... Here are some other samples that I've posted previously that explicitly walk through the creation of two forms and how to pass back-and-forth. Check these out

Manually adding page event handlers in ASP.Net C#

When I've built applications in the past I've used AutoEventWireup to handle the page events for me. From what I've read this incurs a significant performance cost and I'd like to do it manually in my current application.
What is the correct place to set up the event handlers?
My initial thought was to just set up a constructor in my code behind file and do it there but I'm assuming that the auto generated portion of the partial class already contains a constructor that I'd be overriding.
I'm sorry to ask here on such a simple question. It seems like this should be easily searchable but I'm just not finding the answer I need. Thanks in advance for the help.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
Load += new EventHandler(Page_Load);
}
For controls it's OnInit, since they have no OnPreInit. To be honest I've used OnInit for pages as well in the past :)
Of course, you could just do the above for all the events you need for your page, and define no event handlers whatsoever.

How to pass method name to custom server control in asp.net?

I am working on a Customer Server Control that extends another control. There is no problem with attaching to other controls on the form.
in vb.net: Parent.FindControl(TargetControlName)
I would like to pass a method to the control in the ASPX markup.
for example: <c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff">
So, I tried using reflection to access the given method name from the Parent.
Something like (in VB)
Dim pageType As Type = Page.GetType
Dim CallbackMethodInfo As MethodInfo = pageType.GetMethod( "MethodName" )
'Also tried
sender.Parent.GetType.GetMethod("MethodName")
sender.Parent.Parent.GetType.GetMethod("MethodName")
The method isn't found, because it just isn't apart of the Page. Where should I be looking? I'm fairly sure this is possible because I've seen other controls do similar.
I forgot to mention, my work-around is to give the control events and attaching to them in the Code-behind.
If you want to be able to pass a method in the ASPX markup, you need to use the Browsable attribute in your code on the event.
VB.NET
<Browsable(True)> Public Event InitializeStuffCallback
C#
[Browsable(true)]
public event EventHandler InitializeStuffCallback;
Reference:
Design-Time Attributes for Components and BrowsableAttribute Class
All the events, properties, or whatever need to be in the code-behind of the control with the browsable attribute to make it so you can change it in the tag code.
Normally you wouldn't need to get the method via reflection. Inside your user control, define a public event (sorry I do not know the vb syntax so this will be in c#)
public event EventHandler EventName;
Now, inside your aspx page, or whatever container of the user control, define a protected method that matches the EventHandler:
protected void MyCustomerControl_MethodName(object sender, EventArgs e) { }
Now, inside your markup, you can use
<c:MyCustomerControl id="MyCustomerControl" runat=server OnEventName="MyCustomerControl_MethodName">
Your workaround is actually the better answer. If you have code that you must run at a certain part of your control's lifecycle, you should expose events to let the container extend the lifecycle with custom functionality.
buyutec and Jesse Dearing both have an acceptable answer.
[Browsable(true)]
lets you see the property in the Properties window. However, the event doesn't show up, which makes no difference to me.
The thing I overlooked earlier was the fact that when you reference a control's even from the tag, it prep-ends On.
Every ASP.NET page is class of its own inherited from Page as in:
class MyPage : Page
Therefore, to find that method via Reflection, you must get the correct type, which is the type of the page class that stores the page code.
I suppose you need to support multiple pages for this control to be instantiated in I believe you can find the child type of any instance of Page via Reflection, but I do not remember how, but you should be able to do it.
but... like everyone else has said, such case is what events are for.

Categories

Resources