Set session variable vb onclick of hyperlink

Posted on 2009-05-01
Last Modified: 2012-08-14
For various reasons I need  to set a session variable when clicking a hyperlink.  I know this is basic but I can't figure out what I'm doing wrong.

My code is below.  Any help if much appreciated.

My Function: 

<script language="VB" runat="server">

Sub setEventId(Source As Object, E As EventArgs)

    Session("eventId") = sp_EventDetail.FieldValue("eventId")

End Sub



My Link: 

<asp:HyperLink id="link1" NavigateUrl="test2.aspx" target='_self' OnClick="setEventId" runat="Server">Test</asp:HyperLink></ContentsTemplate>

Open in new window

Question by:cbruno33
    LVL 17

    Expert Comment

    What is sp_EventDetail?
    Could you post all code?

    Also, keep in mind that when you click that link, the page does a postback meaning that the page is reloaded, the page_load event is run and then your sub setEventId runs.

    Author Comment

    Hi and thanks for the reply.  

    Attached is code for the entire page and here is a link to the page (sorry it's a bit of a mess).

    Here's what I'm trying to do and maybe there's a better approach.  
    The sp_EventDetail is a dataset (return by a stored procedure) that contains a list of calendar events.  These events are displayed in a table.  My "test" link is included with every row.  When clicked I want to set a session variable named eventId to the eventId of the row with the hyperlink.

    The reason I'm not passing this in the URL is because of a strange situation.  The link will actually go to an .html page.  From the html page, the user will navigate to an aspx page.  When they get to that 2nd html page, I need access to the eventId associated with the calendar event they clicked two pages back.  I thought I could do this using a session variable.  Is there some other way?

    Thanks again.  
    LVL 6

    Accepted Solution


    Try using a LinkButton instead of a Hyperlink. They will look exactly the same and you will be able to use the OnClick event of the LinkButton so you can add the Session variable.


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now