What determines the event - c#

I'm only a beginner so ,my question might sound a bit stupid or basic.
I learn programming in asp.net, therefore I see a lot of functions activated by events. Yet, I didn't find anything in the code nor in the type signature that defines which event activates the function.
So, in functions like public void Page_Load (object sender, EventArgs e), where are the code lines that determine what event will make the function to start? Does it have any relation to the function's name?
Thanks :)

Functions like Page_Load are called by ASP.NET in a particular order. You cannot configure which will fire first. The idea is that you override the ones you need to fire your code in the particular order you need.
Here is the MSDN Page Lifecycle information which talks about which event can be overridden and what order they go in.

In ASP.Net 1.1, we used to have the following system generated code in every code behind files.
public class Default : System.Web.UI.Page
{
// ----- System generated code
protected System.Web.UI.WebControls.TextBox Name;
protected System.Web.UI.WebControls.TextBox Email;
public Default()
{
Page.Init += new System.EventHandler(Page_Init);
}
// ----- System generated code
private void Page_Init(object sender, System.EventArgs e)
{
}
}
It basically registers method to page event. They are nothing but just make the code behind file dirty.
Start from ASP.Net 2, they moved the system generated code to designer file, and code behind file becomes clean and easy read.
public class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
}
-- OR --
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
}
where are the code lines that determine what event will make the
function to start?
ASP.Net uses conversion over configuration approach to register events. It means, you can name a Protected method with following event name, and the page will know how to attach those event. For example, Page_Init, Page_Load and Page_PreRender
In addition, you can override those events explicit if you want.
https://msdn.microsoft.com/en-us/library/ms178472.aspx

Related

How to register event for TextBox end editing

Super newbie to C# (first day coding), so please don't judge me too much if the following is really a stupid question to you.
But I am looking for a way to register an event for when a TextBox end editing, which is similar textFieldDidEndEditing(_ textField: UITextField) delegate in iOS.
After some googling around, I know how to register an textChanged event with the following:
In .xaml file:
<TextBox TextChanged="textChangedEventHandler"/>
In .cs file:
protected void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
}
I also notice this SO, and finally this documentation by MS and notice this following function:
// This method handles the LostFocus event for textBox1 by setting the
// dialog's InitialDirectory property to the text in textBox1.
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
// ...
}
But what is not obvious to me is how do I register this event function? Or how do I let the GUI know to call this function when the TextBox end editing?
This is finally what it takes for it to work:
In .xaml file:
<TextBox LostFocus="textFinishedEditingEventHandler"/>
In .cs file:
public void textFinishedEditingEventHandler(object sender, RoutedEventArgs e)
{
}
Thanks to #Dacker!~
Normally an event can be registered in two ways:
In markup. In ASP.Net each type of event is exposed with the prefix On, so for Click it's OnClick. In your xaml I don't see the On prefix, so that makes me guess it is the following in your case:
<TextBox LostFocus="textBox1_LostFocus" />
In code behind (.cs)
textBox1.LostFocus += textBox1_LostFocus
If you understand this, you can use better names for textBox1_LostFocus to describe more what will happen instead of when it will happen.

Page_Load in C#

I am working with C# web application. I want to know deeply about the page events. Because I thought that the page load event happens first (when a page is requested in browser). But when I tried with commenting the method protected void Page_Load(object sender, EventArgs e) the page get loaded without error.
off-course your webpage will work even if there is no Page_Load() method.
Before a Page_Load() events like PreInit, Init() etc are called. Refer to page life cycle.
Page_Load() method is called after a preLoad event. With Page_Load() you can set default values or check for postBacks etc.
protected void Page_Load(object sender, EventArgs e)
{
int x = 10;
}
write this and put a break-point on int x = 10; watch sender and e.
Every Page object has nine events, most of which you will not have to worry about in your day to day dealings with ASP.NET. The three that you will deal with the most are:
Page_Init
Page_Load
Page_PreRender
They do execute in the order given above so make sure to take that into consideration, especially when building custom controls. The reason you have to keep this in mind is because information might not be available when you expect if you do not deal with it appropriately.
Refer: Life Cycle
1.Page request
2.Start
3.Initialize
4.Load
5.Postback Event Handling
6.Rendering
7.Unload
This is the page life cycle.
Load event comes at 4th position.
You can check details over here:
http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

asp.net override

what are the pros and cons of using a standard event handler or overriding the base class of an asp.net page? Are there any? I've seen both used to do the same thing.
protected void Page_PreInit(object sender, EventArgs e)
{
//Put your code here
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//Put your code here
}
if you use the override you can decide when the custom function should be execute. after or before base method. but if you use auto wireup events it will execute after the base event.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//Put your code here
}
or
protected override void OnPreInit(EventArgs e)
{
//Put your code here
base.OnPreInit(e);
}
First of all, both aren't the same thing.
OnLoad, OnInit and so on are the methods which fires events. Its goal is encapsulating event firing so, if these're virtual methods, derived classes will have the ability of override them and do something before and/or after firing some event.
I wouldn't ask for pros and cons, but "when to use them", because both are different things.
When to use event firing methods
Some operation must be executed before or after some page or control life-cycle.
Some life-cycle step needs to initialize something so page or control subscribers will do something in its correct state by definition.
Authorization: that's preventing the execution of some resources during life-cycle because of security issues.
Add some custom life-cycle step, so page or control needs to notify some subscribers about that.
When to use events directly
Page or control itself, or an associated to control collection or just an observer, needs to do something when page or control is in some life-cycle step.
Pay attention because if you tend to override event firing methods you're modifying the way events are fired themselves, which is a critical thing.
If you need to do something during some page or control life cycle, subscribe to the event, and if you need to subscribe to do something before some event is fired, implement a new event and fire it before next one gets raised:
public event EventHandler CustomEvent;
protected virtual void OnCustomEvent(EventArgs e)
{
if(CustomEvent != null)
{
CustomEvent(this, e);
}
}
protected override void OnPreRender(EventArgs e)
{
OnCustomEvent(new EventArgs());
base.OnPreRender(e);
}
In my opinion, overriding the way an event gets fired when the situation is some object needs to be notified when something happens is a bad usage of C# language, since this is achieved by using event delegation model.
There is much difference in both of the name that you have mentioned, I would like to tell you about that, actually overriding means to use the same method with the same name in the child class, but only the parameters are different in both of the methods, If suppose take the example of the calculating the area, and if you want to calculate the area of two to three different object, then you have to take three different names rather you can use the same name for each, just the different parameters.
Page_PreInit will be called if autoeventWireUp is On.
The func OnPreInit is virtual , and in your page is overrided.
but the master function which is virtual - is executing the code which triggers the Page_PreInit.
so you have to call base.OnPreInit(e); even if you override it.
IF YOU NEED TO PUT SOME CODE BEFORE OR AFTER THE OnPreInit so use the second one .

Problems with IsPostBack

I have an .aspx page which sends a letter to a customer if a button on that page is clicked. Onclick the page calls itself, so the mail send class is in the same file. However I do not want the mail sent when the page is simply loaded. I want it send the letter when the button is clicked, so, I'm trying with the following code:
void page_Load(Object sender, System.EventArgs e)
{
if (IsPostBack)
{
SendMail();
}
}
But it doesn't work. What am I doing wrong?
Why not use the Button's Click Event then?
What you are talking about is you want to send an email only when a specific button is clicked. Then why not register to it's click event instead of bloating your page_load with extra code?
Button's click event is raised only when that button's click causes a postback. So, that's your best option.
Make an event handler for the button's click event (Just double click the button in Visual Studio's Designer).
Using Page_Load will result in emails being sent out when the user posts back in any circumstance, not just your button click.
Looks like the page does not find the correct event handler for the Page_Load, check the case and correct it to Page_Load
See if this works (replace your page_Load method with the following code):
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (IsPostBack)
{
SendMail();
}
}
You are currently relying on AutoEventWireup functionality to hook up your page events. This is slow and problematic and may be the cause of your issue. The method I gave you overrides Page.OnLoad and should correct the problem as well.
The Page_Load event is raised every time the page is posted, well by means of postback or callback, if you want to use server side events you should call you SendMail method in the button's click event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !IsCallback)
{
/*occurs the first time the page is loaded*/
}
if (IsPostBack)
{
/*occurs every time a postback is raised (e.g. by form submission) */
}
if (IsCallback)
{
/*occurs every time a callback is raised, e.g. by generating callbacks by means of AJAX/
}
}
protected void SendMail_Click(object sender, EventArgs e) { SendMail(); }
Read more at MSDN: ASP.NET Page Life Cycle.

Calling my own event code and custom event code in c#

I have made a WinForms application with a custom richtextbox control.
I am referring it as a Windows control (a dll file) in my project. In my program for the richtextbox I have written functionality inside it's textchanged event.
I want to do additional work only after the textchanged event is fired or in other ways once the text is added to the textbox. Something like I want to call a function foo() in the text_changedevent. It only calls foo and fails to process the underlying textchanged event.
Any way in C# I can make it process the internal textchanged event first, and then look into my text changed event?
think of the scenario I have written the code for mytextbox_textchanged
private void txt_code_TextChanged(object sender, EventArgs e)
{
//some code which will always be called whenever textchanged event occurs.
}
Now I inherit this control in my project say MyApp1. Here I have a label where I want to display the number of lines contained inside my textbox. So I would write
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
//code to update the label with my_inherited_txt_code.lines.length
}
so my problem was, I first wanted the txt_code_TextChanged event to be called and then do the code written inside my_inherited_txt_code_TextChanged. Which was solved by writing
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
MessageBox.Show("foo");
}
Do you mean:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// your code here...
}
I cant see why you shouldnt be able to call a method from the text_changed event?
Do you get any errors or what happens exactly?

Categories