Events don't fire in useful order

kkonkle

Newcomer
Joined
Feb 9, 2004
Messages
20
Location
Ann Arbor, MI
I am coming from a few VB projects to help someone work on their ASP.Net project, so I do not have a lot of experience in ASP, or .Net.

We have a web form set up, form1, and on this form is some data, a few buttons, and a tab control. Inside the tab control are 4 more web forms. I believe the tab control is from Infragistics.com.

One of the buttons on the front page is New. When I click this I'd like to clear the main form, and all 4 form within the tab control. The problem is, when I click New it fires all 5 page_load events, and then it fires the button click last. So my pages have no idea I want a new record.

Is there some setting inside ASP.Net I could change so the button click event fires first? I have done pretty well at making my Page_Load events all knowing, since no button click can ever set a flag telling page load it was clicked, but it is just getting too complicated now.

So am I missing something easy here, or is this just the way ASP.Net works? If this is the way it works, how do the rest of you handle this issue?

/<evin
 
You cannot set the order of events, this is normal.

What you should be doing in your form load events is establishing what needs to be executed on post-back and what does not. In other words only certain parts of your code should run when it's the first time the page is loaded and all subsequent times the code should be ignored. To accomplish this use the following in your page load event...

Code:
If Not IsPostBack Then
	'This is the first time in this page
	'Load a bunch of data orr controls.
Else
	'This is from a button click or a textbox change or dropDownList AutoPostBack
	'Do something else here
	'NOTE: If let's say it's a button click then don't
	'do anything here, handle it in your button click event.
End If
 
Robby said:
You cannot set the order of events, this is normal.

What you should be doing in your form load events is establishing what needs to be executed on post-back and what does not. In other words only certain parts of your code should run when it's the first time the page is loaded and all subsequent times the code should be ignored. To accomplish this use the following in your page load event...

Code:
If Not IsPostBack Then
	'This is the first time in this page
	'Load a bunch of data orr controls.
Else
	'This is from a button click or a textbox change or dropDownList AutoPostBack
	'Do something else here
	'NOTE: If let's say it's a button click then don't
	'do anything here, handle it in your button click event.
End If

That would be fine if I had a single page that was effected, but because we're using this tab control object we have 4 pages nested within the main, front page.

So yes, on not postback I do a bunch of init stuff, and otherwise I handle filling in of data.

I cannot handle the filling in of data in the button click event because I don't have access to those other pages. It has been awhile since I did any OOP, but I seem to recall a class can have Public functions. I tried making a Public Shared function in each of the pages that would fill the data, and then I could just call those 4 functions in my click event, but even though the functions are public, it will not let me access protected members on other forms. And if I change the declarations of those members to Public Shared, I can access them from other forms, but after I rebuild and save, ASP.Net sets all of them back to Protected WithEvents.

So I click load, my front page load event fires, then the 4 nested page load events fire, then my load_click event fires. I could load the front page in the click event, but the other 4 pages have already loaded and contain the wrong data.

But, I have been searching all morning and I found a solution. In my main, front page, in the page_load event I will put something like this:

Code:
    If Not Page.IsPostBack then
        'Do Init stuff here
    Else
        If Len(Request.Form(2)) > 8 Then
            TextBox2.Text = Request.Form(4) & " was clicked"
        Else
            TextBox2.Text = Request.Form(2) & " was clicked"
        End If
    End If

Request.Form(X) returns the name of the button that fired the postback event. The string is sometimes in index 2, and sometimes in index 4, hence the If statement. Since this is the first page_load event that fires, once I know which button was clicked I can set a session variable, so when the other 4 pages load they will know why it is they are reloading.

This seems like a pretty ugly way to do this. Can someone tell me what I am doing wrong with the public functions? Why does ASP.Net insist every textbox, listbox, and radio button be Protected WithEvents? Is there a way to easily read a protected TextBox1 on Form2 by calling a function from Form1?

/<evin
 
The Request.Form is indeed an ugly way to go, of course there are work-arounds to every problem but in this case I think a re-design may be better suited.

Are any of these 4 nested pages ever called directly from a client browser? If not then perhaps you can create user-controls from them or place their child controls within panels on the same page.

All this to say; is there no way for you to do all this from a single aspx page?
 
Robby said:
The Request.Form is indeed an ugly way to go, of course there are work-arounds to every problem but in this case I think a re-design may be better suited.

Are any of these 4 nested pages ever called directly from a client browser? If not then perhaps you can create user-controls from them or place their child controls within panels on the same page.

All this to say; is there no way for you to do all this from a single aspx page?

Yeah, it isn't the best solution, but we just don't have time for a redesign at this point. I did tweak the Request.Form function I listed above, so it is a little more robust, but that is beside the point.

We are using tabs because there is a lot of data and we didn't want a single form that was several pages long. Plus, there is an internal side to this program and we were trying to keep the look and feel the same.

A simple public function on each of the 4 nested forms that would let me fill in the data would be a lot cleaner. Is my project too far gone to incorporate that, or is that something I could modify a couple things and then implement?

/<evin
 
The way I see it it's not too far gone, converting each page into a user-control would take a few minutes and the long-term savings would be recognized immediately.
 
Robby said:
The way I see it it's not too far gone, converting each page into a user-control would take a few minutes and the long-term savings would be recognized immediately.

I talked with the programmer who set this project up. Apparently the 4 forms within the tab control are already web user controls.

Like I said in my initial post, I am coming from VB and do not have but a couple weeks experience in ASP and .Net. So what advantage do the web user controls give me?

/<evin
 
Back
Top