We have an old ASP.NET WebForms site that only works in Microsoft Edge’s IE Mode because of the hundreds of popup windows, often 10 per page. It needs to be rewritten, but that is a long-term project that has not been approved resources yet.
-
Page 1 has a popup dialog, called Page 2.
-
The popup Page 2 also has a popup dialog, called Page 3.
-
Values selected on Page 3 need to update Page 1.
Page 3 writes the Session variable when a DataGrid item is selected:
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
' Set Search Info
If dgInvoice.SelectedIndex > -1 Then
Session("csrWhere") = String.Format("WHERE ClaimKey = '{0}'", dgInvoice.Items(dgInvoice.SelectedIndex).Cells(15).Text)
End If
End Sub
I created a JavaScript timer to start as soon as Page 1 is loaded, and it looks for the Session variable to be set.
This is my Page 1 HTML:
<form id="Form1" method="post" runat="server">
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
var csrWhere = '<%=Session("csrWhere")%>'
if (csrWhere != '') {
alert(csrWhere);
location.reload();
}
}, 200);
});
</script>
Page 1’s Page_Load event reads, uses, and clears the Session variable:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
If Session("csrWhere") IsNot Nothing Then
Databind(Session("csrWhere"))
Session("csrWhere") = Nothing
End If
' other code
Whenever I select an item on Page 3, the Session variable is set, but Page 1 never reloads.
The JavaScript does not seem to be working. I added the alert()
, but that never happens.
I recently learned in my question before that IE Mode does not have Developer Tools, so I am having trouble debugging.
Why is the JavaScript not firing?
2
Answers
Keep in mind that setting a text box in page load to some session value, or using an "in line" expression is ONLY evaluated on page load, then the WHOLE page is sent to the client side. On server side, the page is blowen out of memory, disposed of, and now you have a "static" rendered page just sitting on the user’s desktop. As such, be it a text box, or some in-line server expression (which amounts to a ONE-TIME replacement operation), the page content on the client side is now static, and such expressions, or even textboxes etc. don’t by some act of magic update for you. Your code behind, or even those "<%= some server-side expression goes here %> is only changed and modified at page load time, and THEN the whole page is sent to the client side with those values. So, server-side code does not out of the blue go now back into some browser page sitting on the user’s desktop, and update those textboxes, or controls or in this case some server-side expression that was rendered ONE time at page create time.
So, if you going to poll the session() value, then you need to re-load the page, or perhaps create a web method in that page, and have JavaScript call that page method to get the updated value of session().
So, once the text boxes, server side "in line" expressions been evaluated, then such expressions don’t re-update until such time a whole new copy of the browser is brought down from the server. Such values and expressions are evaluated ONE time before the WHOLE page is then sent down to the client side.
Obviously, this means that such expressions will not change since the last post-back and so called "round trip".
So, you would have to add a web method to the page, and have the client-side JavaScript "query" or "pool" that server-side code (as a web method) over and over.
Hence, you could say (assuming jQuery) add this code to your page 1.
Then in client-side JavaScript, you could poll (call above) say every one second to get the session value.
The JavaScript (jQuery) .ajax call would thus look like:
So, all rendering and even the server side "<%= %>" expressions are ONLY rendered and occur only at page creating time, not that a whole bunch of values on the page are now automatically being updated by the server in real time.
A grasp of the asp.net page life cycle is really quite much required to grasp how web development works. Server-side code (code behind), and that includes those in-line expressions only modify a copy of the web page, and ONLY do so when the page is posted back to the server. Code behind changes are not thus seen by the user, but ONLY can be seen when a WHOLE NEW fresh copy of the browser is sent to the client side.
Of course, the above routine would have to be wrapped in a client side set Interval timer for it to run over and over.
There is a problem with this line of javascript:
The
csrWhere
Session value is expected to include single quotes, such that you’ll end up with invalid javascript syntax in the final rendered page. For example, if someone selects a row with the letter "A" in cell 15 to use as aClaimKey
, you’d end up with a javascript statement like this:And this clearly has mismatched quotes, so it’s not valid javascript.
Worse, processing SQL in this way represents a HUGE GAPING SECURITY ISSUE and you should be grateful the site hasn’t been hacked already. Probably the
DataBind()
method needs to be refactored, and this needs to be a top priority.Separately, I see this:
Remember, with web forms, the entire page is rebuilt from scratch for every event. Since Page 3 is popup within Page 1, I promise Page 1 had to reload for the
cmdGo_Click()
event method to fire. But.. the event methods come well after thePage_Load()
event in the page life cycle, and so that load code has already completed by the time we set the variable. Depending on how the method works, you may by able to get around this simply by callingDataBind(Session("csrWhere"))
again.