Search code examples
asp.netvb.netpageloadpage-lifecycle

How can I specify code to run on "PageLoad" in an old ASP.NET site?


I am maintaining an old site that has code like this:

<%
Unit = Request.QueryString.Item("Unit")
MemberNo = Request.QueryString.Item("MemberNo")
CustNo = Request.QueryString.Item("CustNo")
If Request.Form.Item("Action") = "Save" Then
. . .
%>

I need to run some code when the page loads. Is there something available like:

If Request.Form.Item("Action") = "Load" Then

--or:

If Request.Form.Item("Action") = "Init" Then

...or how do I do this?

UPDATE

Where should I put that code you show? The file has a section like this:

<script language="VB" runat="Server">
Dim adoRS As ADODB.Recordset
. . .
</script>

...and another like this:

<%
adoCon = New ADODB.Connection
adoCon.CommandTimeout = 900
adoCon.ConnectionTimeout = 900
adoCon.Open((Session("DBAddress")))
%>

Is one place preferable than the other, or even exclusively the only one to use? Or is neither acceptable?

Actually, if I put the code in the first block, I will have to move the code from the second block there, too, because I need the ADODBConnection for the PageLoad code.

UPDATE 2

Ciprian's answer said:

If the aspx.vb page does not exist, check the first line in the .aspx file. It should look something like this:

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default...

My .aspx file has this at the top:

<%@ Import namespace="ADODB" %>
<script language="VB" runat="Server">
. . .

IOW, I have no such first line as Ciprian expects I should have. The project also has no "Site.master" page.


Solution

  • It turns out all I needed to do was just to put it at the top of the code, like so:

    <%
    Unit = Request.QueryString.Item("Unit")
    MemberNo = Request.QueryString.Item("MemberNo")
    CustNo = Request.QueryString.Item("CustNo")
    
    'determine whether this unit is a new business
    currentYear = Year(Now)
    SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
    adoRS = New ADODB.Recordset
    adoRS.Open(SQLString, adoCon)
    IsNewBusiness = TRUE 'default (if record not found)
    If Not adoRS.EOF Then
        IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 
        Response.Write("<!-- IsNewBusiness after NOT EOF assignment = " & CStr(IsNewBusiness) & "-->")
    End If
    adoRS.Close()
    
    If Request.Form.Item("Action") = "Save" Then
    . . .