Solved

How do I run some code on the loading of a page (ASP)

Posted on 2004-09-20
7
229 Views
Last Modified: 2010-04-17
I am trying to use cookies and I am new to web page development.  My thought is that when the page loads I should check for my cookie and adjust the formatting of the page acordingly.  So if a user is loged in certain fields will be availible and some will not.  I have figured out the cookie thing mostly but I cant figure out how to get the code to run before a button on the page is clicked.  I need to run my statements before the user sees what is on the page - before a button is clicked.  Where? How? do I put in the code I need?

Here is a tidbit from the start of my page...


Thank you,

TH

CODE
======================================================================================================

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
   
                                                                                       ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                                       ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        If Request.Cookies("Preferences").HasKeys Then         ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            TextBox_Password.Visible() = False                       ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            TextBox_UserName.Visible() = False                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        End If                                                                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                                      ' <------------------ need to run this code first  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
    Sub Button_Submit_Book_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       
        'gather user entry data
        Dim strUserName As String
        Dim strPassword As String
       
        If Request.Cookies("Preferences").HasKeys() Then
           
            strUserName = Request.Cookies("Preferences")("UserName")
            strPassword = Request.Cookies("Preferences")("Password")
        End If
       
       
       
        If Not Request.Cookies("Preferences").HasKeys Then
            strUserName = TextBox_UserName.Text
            strPassword = TextBox_Password.Text
        End If
       
        Dim strBook_Title As String = TextBox_Book_Title.Text
        Dim strBook_Auther As String = TextBox_Book_Author.Text
        Dim strBook_ISBN As String = TextBox_Book_ISBN.Text
       
       
        Dim Flag_account_password_exist As Boolean
       
       

Thank you,
0
Comment
Question by:travishaberman
  • 4
  • 3
7 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 12109089
Hi travishaberman,

Don't put your code in <script runat="server"> Put them between <% and %>. This way, all your server side code is executed before anything should be printed to the screen.

Regards,
Zyloch
0
 

Author Comment

by:travishaberman
ID: 12109117
That worked Great!!!   You are going to get the 250... But I am going to up this to 500 for you if you can do me one better and tell me the syntax to check and see if a cookie exists..   My problem is that my current code checks to see if the cookie has "Keys"

If Request.Cookies("Preferences").HasKeys() Then
...
...


What I want to do is check to see if the cookie is there at all..

any thoughts?
if no.. then I will just accept your last answer..

Thanks!

-TH
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12109130
Never doubt the power of Google.

You can check

Request.Cookies("Preferences")<>''

if I'm correct, but if you want to check if a Preferences cookie has a certain key, you'd do

Request.Cookies("Preferences")("keyName")<>''
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:travishaberman
ID: 12109148
hmmm.. that didn't quite do it.. I tried, but it says.. but I get an "expresson expected"

any more thoughts?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12109155
Ah, let's see...

(Playing around here since not TOO great at ASP)

If Request.Cookies("Preferences") Is Nothing Then
   Cookie Does not Exist
0
 

Author Comment

by:travishaberman
ID: 12109164
That did it!!!

Thank you much,

-TH
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12109168
No prob, glad to help :)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question