473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the Button_Click Event before Page_Load?

Hi Folks,

I have a problem with the order of events fired by ASP.NET. I found many
articles which explaining the lifecycle of a site, but I found none
which took the event from a Control on the site into consideration.

Here is what I want to do:
I have a Button, which starts a new search session. When this session is
started, you have several Usercontrols, which can navigate within the
search session.
So: If the button is clicked, a new search session is started. I do not
need to care about any previous states.
If the button is *NOT* clicked, I have to build up the site from some
Session-Variables, which I have saved bevor.

Unfortunatly, the Button_Click event is the last in line. This would
mean, that I need to build up the site first in Page_Load and then, in
Button_Click, redo all my work and init a new search session.

So is it possible, to get to know, if the button was clicked *before*
all other Page-Events are fired?

Thanks,
Frank
Nov 18 '05 #1
2 2537
Page_Load is used to load the page. If you are using it to set up items that
a page needs when loaded, you will not have a problem with event order.
AFAIK, you cannot change the order of events.

The easiest way to avoid issues is only set page state for when IsPostback =
false. Then, you will not collide with items in Page_Load when you click a
button. This may require a bit of rearchitecture.

Consider moving the code that determines state into its own routine. When
IsPostback = false, call the state to pull from Session vars. When button
click, IsPostback = true, so call a routine to drop session from button click
and then call the routine that determines state. The basics of the code is
something like this:

public void Page_Load(objec t sender, EventArgs e)
{
if(!Page.IsPost back)
SetControls(tru e);
}

private void SetControls(boo l IsInSession)
{
if(IsInSession)
//Set controls from Session
else
//Set controls with new state
}

public void Button1_Click(o bject sender, EventArgs e)
{
//Kill session
Session.Abandon ();

SetControls(fal se);
}

Too often, the Page_Load event is used as Control Central, which is a
mistake. Unfortunately, the mistake is propagated by many books on the market
that use an else condition on if(!Page.IsPost back).

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** ************
Think Outside the Box!
*************** ************
"Frank Schumacher" wrote:
Hi Folks,

I have a problem with the order of events fired by ASP.NET. I found many
articles which explaining the lifecycle of a site, but I found none
which took the event from a Control on the site into consideration.

Here is what I want to do:
I have a Button, which starts a new search session. When this session is
started, you have several Usercontrols, which can navigate within the
search session.
So: If the button is clicked, a new search session is started. I do not
need to care about any previous states.
If the button is *NOT* clicked, I have to build up the site from some
Session-Variables, which I have saved bevor.

Unfortunatly, the Button_Click event is the last in line. This would
mean, that I need to build up the site first in Page_Load and then, in
Button_Click, redo all my work and init a new search session.

So is it possible, to get to know, if the button was clicked *before*
all other Page-Events are fired?

Thanks,
Frank

Nov 18 '05 #2
Hi Cowboy,

thanks for your reply!

Your answers gave me some insight into my problem, but I need to think
it over a bit more. But anyway, I think you have pointed me in a good
direction, from which I can go further.

Ciao,
Frank
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2567
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their details, their locations, and then add themselves to categories. Each category requires additional info from the suppliers, this additional category info is stored in its own DB table. a suppliers may add themselves to as many categories as required....
7
3265
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
0
1158
by: Tim Zych | last post by:
I'm having problems with a dropdownlist control. I fill the control with values in the page_load event. But when I click the button the dropdownlist control gets cleared out. How do I use a value from the dropdownlist? I've simplified what's going on. This example is supposed to add to a textbox the value in the dropdownlist.
2
1498
by: Skating_vince | last post by:
I have a problem, i have a form with some input boxes and a submit button. In the Submit_Click event, the data from the input boxes is stored in a database. But, if you click the button, ASP.NET will first load the next Page_Load and when that is done, it will do the Submit_Click event. So on the page you don't see the data from the database, because it is not there yet. When you refresh the page, the data is there, but it has to be...
2
1489
by: David Thielen | last post by:
Hi; The examples I have seen all use "if (IsPostBack) { ... }" to handle an action when the user presses the submit button. However, it seems to me a more object oriented approach is to use a callback event attached to a button. Is there a good reason to use one over the other?
2
3589
by: VMI | last post by:
I have a LinkButton_search on my Page1.aspx that opens up a popup page called popup.aspx. I do this with LinkButton.Attributes.Add() on the Page_Load of Page1.aspx. How can I add server-side code to LinkButton_search_Click() so that the code in there runs before opening the popup window? The problem is that I fill a Session variable when I click on the button that will then be used in the Page_Load of my Popup window. So basically this is...
0
1909
by: John | last post by:
I am loading my controls dynamically into my asp.net page. When I click on a button on one of those user controls then page reloads and I need to reload the page based on what has happened in the button click. Problem is that the button click is the last thing to fire so I need to reload the page a second time to update the controls based on the logic in the button click. The second time around the page loses its viewstate which stores...
1
1605
by: =?Utf-8?B?VmlrcmFt?= | last post by:
is there any object available in c#/asp.net which can give me event name in that eevnt only. Like if I am in Page_load event, i should get Page_Load or if I am in Button_click event I should get Button_Click. SO that I can logg it in my cutom log file with a generic logic. Thanks
2
1137
by: Cindy Lee | last post by:
I have an image button that I do an button_click with protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { stringt value1=TextBox1.Text; I get the value here ok, but only if I don't set it in the pageload part. TextBox7.Text="a"; // if this is in the pageload, I will always get "a" I want to be able to change the value on the page, and have it show up in
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.