J
John Kotuby
Hi guys,
I am converting a rather complicated database driven Web application from
classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The
original ASP application works quite well, so at times it is tempting just
to port parts of it over mostly as-is. In fact, one MSDN article I read
suggested using straight HTML wherever possible to make the app more
efficient and less resource demanding.
On one page there are 2 Dropdown listboxes written in Classic ASP where we
see onchange="Filter()", referring to a clientside JavaScript function that
simply posts back to the originating page with a querystring name/value pair
of FilterIt="Y". The original page load runs an unfiltered SQL Server query
to display records. When a user makes a selection from the dropdown list,
the postback page looks for Request("FilterIt")="Y" and then proceeds to
re-run the query with the filter selected by the user from the dropdown. It
works quite nicely. (The original classic ASP)
However, I have decided to use the Server DropDownList control to more
cleanly separate the SQL query from the actual page generation code. When an
event is captured clientside, the event handler fires immediately. Therefore
I logically deduced that the same would be true of a server side event
handler immediately upon postback. At least, that is what I hoped would
happen.
My plan was to set a Public variable FilterIt to "Y" in the even handler for
either of the dropdownlists. Here is an example of the code that calls the
handler from one of the controls:
-------
<aspropDownList runat="server"
ID="catList"
OnSelectedIndexChanged="mlrList_SelectedIndexChanged"
AutoPostBack="true">
-----
In the handlers I set the Public variable FilterIt="Y" and also send out
some debugging code, and I do not reset the value of FilterIt before the
Page_Load event.
-----
Protected Sub catList_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles catList.SelectedIndexChanged
filterIt = "Y"
'*** JPK debug
Response.Write("2 catList_Selected: cat=" & catList.SelectedValue.ToString()
& " filterIt=" & filterIt)
Response.Write("<br/>")
'*** JPK debug
End Sub
-----
Note how I placed a number before the response text to indicate what I
expect to be the firing order of events. The other dropdownlist event
handler has a 1 preceeding the Response text.
Now in the GatherRecordsets() Sub called from Page_Load I make my SQL query
calls. I understand that is the suggested location for gathering data. Here
is what I have done prior to assmebling the SQL query string:
-----
If Not Page.IsPostBack Then
filterIt = "N"
mlrFilter = ""
catFilter = ""
Else
mlrFilter = mlrList.SelectedValue.ToString()
catFilter = catList.SelectedValue.ToString()
End If
'*** JPK debug
Response.Write("3 GetRecordSets: filterIt=" & filterIt & " mlrFilter=" &
mlrFilter & " catFilter=" & catFilter)
Response.Write("<br/>")
'*** JPK debug
-----
I do Not set filterIt="Y" on every postback, because not every postback is
requesting a filter.
To my surprise, after selecting a filter category from the dropdown, this is
what I see returned at the top of the page.
-----
3 GetRecordSets: filterIt= mlrFilter= catFilter=electronics
2 catList_Selected: cat=electronics filterIt=Y
2 catList_Selected: cat=electronics filterIt=Y
-----
It appears that the Page_Load event is firing before the event handler. I am
correctly identifying the PostBack because the variable
"catFilter=electronics" is holding the selected value. But even though
"FilterIt=Y" is correctly set in the event handler, since the event appears
to fire after Page_Load, that value never gets to my SQL query.
Also, it seems that the event handler is firing twice. Very strange.
Sorry about the lengthy post, but I wanted to be clear about what is
happening. Is this the intended behavior envisioned by the ASP.NET
designers, such that I have to program a "work-around" to obtain my
objective? Or am I doing something wrong? When I was reading about page life
cycle event firing order, I didn't notice that event-handlers took a back
seat to other events.
Thanks to all for any and all input ...
I am converting a rather complicated database driven Web application from
classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The
original ASP application works quite well, so at times it is tempting just
to port parts of it over mostly as-is. In fact, one MSDN article I read
suggested using straight HTML wherever possible to make the app more
efficient and less resource demanding.
On one page there are 2 Dropdown listboxes written in Classic ASP where we
see onchange="Filter()", referring to a clientside JavaScript function that
simply posts back to the originating page with a querystring name/value pair
of FilterIt="Y". The original page load runs an unfiltered SQL Server query
to display records. When a user makes a selection from the dropdown list,
the postback page looks for Request("FilterIt")="Y" and then proceeds to
re-run the query with the filter selected by the user from the dropdown. It
works quite nicely. (The original classic ASP)
However, I have decided to use the Server DropDownList control to more
cleanly separate the SQL query from the actual page generation code. When an
event is captured clientside, the event handler fires immediately. Therefore
I logically deduced that the same would be true of a server side event
handler immediately upon postback. At least, that is what I hoped would
happen.
My plan was to set a Public variable FilterIt to "Y" in the even handler for
either of the dropdownlists. Here is an example of the code that calls the
handler from one of the controls:
-------
<aspropDownList runat="server"
ID="catList"
OnSelectedIndexChanged="mlrList_SelectedIndexChanged"
AutoPostBack="true">
-----
In the handlers I set the Public variable FilterIt="Y" and also send out
some debugging code, and I do not reset the value of FilterIt before the
Page_Load event.
-----
Protected Sub catList_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles catList.SelectedIndexChanged
filterIt = "Y"
'*** JPK debug
Response.Write("2 catList_Selected: cat=" & catList.SelectedValue.ToString()
& " filterIt=" & filterIt)
Response.Write("<br/>")
'*** JPK debug
End Sub
-----
Note how I placed a number before the response text to indicate what I
expect to be the firing order of events. The other dropdownlist event
handler has a 1 preceeding the Response text.
Now in the GatherRecordsets() Sub called from Page_Load I make my SQL query
calls. I understand that is the suggested location for gathering data. Here
is what I have done prior to assmebling the SQL query string:
-----
If Not Page.IsPostBack Then
filterIt = "N"
mlrFilter = ""
catFilter = ""
Else
mlrFilter = mlrList.SelectedValue.ToString()
catFilter = catList.SelectedValue.ToString()
End If
'*** JPK debug
Response.Write("3 GetRecordSets: filterIt=" & filterIt & " mlrFilter=" &
mlrFilter & " catFilter=" & catFilter)
Response.Write("<br/>")
'*** JPK debug
-----
I do Not set filterIt="Y" on every postback, because not every postback is
requesting a filter.
To my surprise, after selecting a filter category from the dropdown, this is
what I see returned at the top of the page.
-----
3 GetRecordSets: filterIt= mlrFilter= catFilter=electronics
2 catList_Selected: cat=electronics filterIt=Y
2 catList_Selected: cat=electronics filterIt=Y
-----
It appears that the Page_Load event is firing before the event handler. I am
correctly identifying the PostBack because the variable
"catFilter=electronics" is holding the selected value. But even though
"FilterIt=Y" is correctly set in the event handler, since the event appears
to fire after Page_Load, that value never gets to my SQL query.
Also, it seems that the event handler is firing twice. Very strange.
Sorry about the lengthy post, but I wanted to be clear about what is
happening. Is this the intended behavior envisioned by the ASP.NET
designers, such that I have to program a "work-around" to obtain my
objective? Or am I doing something wrong? When I was reading about page life
cycle event firing order, I didn't notice that event-handlers took a back
seat to other events.
Thanks to all for any and all input ...