Calling a function before Page_Load - c#

I have a button that calls function A()
When I click on it I want the calls to be made in that order:
A()
Page_Load()
Right now it's doing:
Page_Load()
A()
Is there a way around that or is it just by design and there's nothing I can do about it?

The easiest way to do this would be to use a HTML Submit button and check to see if it is in the Form on every postback in Page_Init
public void Page_Init(object o, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.Form["MyButtonName"]))
{
A();
}
}
And in your ASP.NET code:
<Button Type="Submit" Name="MyButtonName" Value="Press Here To Do Stuff Early!" />
I think that will work.

Control events (such as the click events of buttons) are called after page_load. The controls are not guarenteed to be fully initialized prior to page_load. If you really need to call a function before page_load has been called based on whether a button has been pressed you'll have to examine the request to check if the button has been pressed (basically old school ASP)

You need to call your function in the Page_Init. Page_Init will happen before Page_Load.
Here's an Overview of the ASP.NET Page Lifecycle.

Not exactly: ASP.NET will always call Page_Load before handling postback events like Button_Click.
However, you can accomplish what you want by redirecting to your page after handling the postback event. (Using the Post-Redirect-Get pattern.)
Inside your Page_Load method, you can avoid running any relevant code twice by checking to see if it's a postback first:
if (!this.IsPostBack) {
// Do something resource-intensive that you only want to do on GETs
}

As Jeff Sternal answered, The Post-Redirect-Get pattern is a good way of solving a problem like this.
In my circumstances i had a calendar and if you clicked a date it would add that to a scheduler. The scheduler would have buttons on each new date that needed to have onclick functions tied to them.
Because the new row was being added with a linkbutton(on the calendar), in the code the new scheduler date was being added at the Postback event handling meaning that the new set of buttons wouldn't have a command tied to them.
The page life Cycle
Post Get Redirect

I don't think it's possible, at least, not in the way described by your question. When you click a button it will send a request to the server which in turn will start processing it, and follow the ASP.NET Page Lifecycle as posted by Joseph.
Alternatively you could try making an AJAX call to a page without reloading the current one you're on and do whatever processing you require.

This is what you want to do for Page Init is called before Page Load.
Take a look at the ASP.net Page Life Cycle
public void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//CALL YOU FUNCTION A()
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}

If your actual goal here is to have your "page loading code" happen after your event handler runs -- for example, if clicking your button changes something in your database, and you want the updated data to be reflected on the page when it loads -- then you could have your "page loading code" get called from a method that gets called later in the ASP.NET page life cycle than your event handler, such as Page_PreRender, instead of calling it from Page_Load.
For example, here's a simplified excerpt from an .aspx.cs page class that has a button event handler that runs before the page population logic, and a confirmation message that is visible on the page only after the button was clicked:
// Runs *before* the button event handler
protected void Page_Load() {
_myConfirmationMessage.Visible = false;
}
// Runs *after* the button event handler
protected void Page_PreRender() {
// (...Code to populate page controls with database data goes here...)
}
// Event handler for an asp:Button on the page
protected void myButton_Click(object sender, EventArgs e) {
// (...Code to update database data goes here...)
_myConfirmationMessage.Visible = true;
}

Related

How can I run a event handler before Page.IsPostBack

I want to run code in a button event handler but the if(Page.IsPostBack) conditional is running first and contains a redirect so the event never runs.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
(do some stuff with save button pressed...)
// then return the same page to prevent another post on refresh
Response.Redirect(Request.Url.AbsoluteUri);
}
}
and on that page there is another button that calls a handler:
protected void Export_Click(object sender, EventArgs e)
{
(do other stuff..)
}
Is there a way to run the handler first or a way to check which button was clicked so I can add a conditional to the redirect?
Thanks.
This behavior is actually by design in WebForms. To keep things stateless, a full postback occurs whenever you wish to fire off a server-side event.
So the short answer is no, you cannot force your event to occur before the Page_Load does.
However, you can restructure your design a bit to get the behavior you desire. At the moment, it looks as though you are depending on a PostBack having a occurred to execute your save logic. Instead of handling the logic in your Page_Load event, it could be handled in an event appropriate to how your user is expecting to save.
If saving is triggered through a button press, then moving logic for handling the save and redirect to a Save_Click event will allow other events related to PostBack to execute their own logic as well.

On the same Page When I click Save_button, firstly Page_Load event should execute or btnSave_Click button?

In my Asp.Net Web Form,
Assuming that, I have Main_page and Save_button that it is on Main_page.
When I click Save_button, firstly Page_Load eventexecute and after this btnSave_Click button execute.
I thought that when I click button firstly and only button might execute.
Is it correct or my program doesn't work correctly ?
It is normal that Page_Load is executed first and event handlers afterwards. So your program behaves as designed.
Excerpt from MSDN on the page lifecycle:
Load
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handling
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
If you are interested in details on the lifecycle of an ASP.NET Page, have a look at this link.
Resolution
If you need to perform certain steps in Page_Load (or any other method on your page) only when the page is requested first, you can check the IsPostBack property and thus make your program behave as you describe in your question:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Steps are only run on initial GET, not when request is a PostBack.
}
}
It is correct that load event fires first then button event handler. If you want to execute code only at first, but not when any postback check IsPostBack property.

server side click event doesn't fire on LinkButton when PostBackUrl property has been set to another page

I put a linkbutton control named as "Logout" on my webpage with VS 2010.
When users press the "Logout" linkbutton , I want the system to do two things.
First is to trigger a server side click event to do some things such clear all session variables etc..
Second is to redirect to user to another page such logon.aspx
So I wrote following codes
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
LinkButton1.PostBackUrl = "LogOn.aspx";
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Abandon();
....
....
}
But when the programs runs , any user clicks the linkbutton.The codes of LinkButton1_Click never be executed, because the page has already been redirect to Logon.aspx page and never do the LinkButton1_Click()
My Question is why LinkButton1 offers PostBackUrl property and server-side click event , but it seems that they don't cooperate very well ~
This is because by setting the PostBackUrl you are saying that when the button is clicked that you want it to post to the LogOn.aspx page rather than posting back to itself. Since you are posting to LogOn.aspx you will never trigger the event.
Instead, you should use some type of redirect in your button click after your sesion.abandon.
Can you check if there's an event LinkButton1_Click subscribed to a LinkButton1's OnClick?
Then for your second concern since you're referring with ASP.Net, you can handle this with your Global.asax, where you will redirect any user to a login page every time session was destroyed. See reference links below:
1.) SessionStateModule.End Event
2.) Using Session in Global.asax

Creating an event for a textbox and AJAX Update Panel Control

I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

Making an EventHandler delegate association in (!IsPostBack) doesn't work even on first page load --- why?

Works:
protected void Page_Load(object sender, EventArgs e)
{
myButton.Click += new EventHandler(myButton_Click);
}
Doesn't work:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myButton.Click += new EventHandler(myButton_Click);
}
}
Now, what I expected in the second example was for the eventhandler to be wired to the button only when it is not a postback (i.e. the first time the page is loaded), and then on any postback, the button would no longer run the method associated with the event. That doesn't seem to be the case --- from the very first load (not a postback), the button does nothing when clicked.
My suspicion is that this is related to the page life cycle --- but I'm not quite sure where this falls into that. If I understand correctly, the method associated with the event gets run after the page has posted back (even if you clicked it on the first time the page loads), but I'm referring to the wiring up of the event to the method with the EventHandler delegate, not the actual running of the associated method.
Note: this is purely an attempt to gain a better understanding of what's going on behind the scenes, and not an attempt to solve a real world problem.
You need to think about how the code is executed with each page request. What happens on the server is that a class instance is created for each request and if the line of code
myButton.Click += new EventHandler(myButton_Click);
is conditional it means the event handler is not wired to the event on postback.
In other words if you write something like
<asp:Button ID="myButton" runat="server" OnClick="myButton_Click" />
This translates to the code you wrote and the event gets wired with each request.
The problem is, the event handler can only trigger when it IS a postback. The wiring of event handlers affects this instance of the class only. When a postback occurs, a new instance of your page is created and the event handler is no longer wired. You need to wire it in order for it to get triggered.
The best thing to do is always wire the event handlers. There's no reason not to.
If it is not a Postback, it means that the user just requested a page (HTTP Get request) with typing the URL in the browser (or clicking a link, or...).
If the user clicked on a button, then the browser makes a HTTP Post request, which on the server side, ASP.NET marks the page that is requested with setting the property IsPostBack to true of the page object.
See the answer on this question.
I guess what you intend to do is disable the button on postback.
You can do that by setting the enabled property to false.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
myButton.Enabled = false;
}
}

Categories

Resources