Solved

How do I move tracking code from master page to an aspx page without a master

Posted on 2008-10-21
5
248 Views
Last Modified: 2011-10-19
I'm in the process of integrating a third-party forum application into my asp.net 3.5 website.  I'm using Visual Studio 2008.  I'm new to .net development but have gotten the basic functionality of the forum to work.  I've been unable to integrate the forum pages with the master page of my main site, but that's an issue for another time.  Right now I'm trying to add tracking code directly to each forum page.  The code works successfully on the master page, but since the forum pages are not code-behind, I'm not sure exactly how to add it to the aspx page.

The code snippet contains the tracking code and the attachment contains the aspx file I'm adding it to, with extension changed from aspx to txt
thanks
Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim SessionDate As DateTime = Now
        Session("SessionDate") = SessionDate
        SessionDate = CType(Session.Item("SessionDate"), String)
 
        Dim sIPAddress As String
        sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If sIPAddress = "" Then
            sIPAddress = Request.ServerVariables("REMOTE_ADDR")
        End If
 
 
        Dim SCRIPT_NAME As String = Request.ServerVariables("SCRIPT_NAME")
        Dim HTTP_USER_AGENT As String = Request.ServerVariables("HTTP_USER_AGENT")
        Dim HTTP_REFERER As String = "ref: " + Request.ServerVariables("HTTP_REFERER")
        Dim AspSessionId As String = HttpContext.Current.Session.SessionID
        Dim IpAddress As String = sIPAddress
 
 
 
        Dim PageFile As String = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)
        Dim UserId As String = "anonymous"
 
 
 
        If (Request.IsAuthenticated = True) Then
            UserId = Membership.GetUser.ProviderUserKey.ToString()
        End If
 
 
        ' Insert a new record into Sessions
        If AspSessionId = HttpContext.Current.Session.SessionID Then
            Dim connectionString As String = ConfigurationManager.ConnectionStrings("SQL2005_529500_havConnectionString1").ConnectionString
            Dim updateSql As String = "INSERT INTO UserSessions (AspSessionId, IpAddress,http_referer,pagefile,UserId,script_name,http_user_agent) Values (@AspSessionId, @IpAddress,@http_referer,@pagefile,@UserId,@script_name,@http_user_agent)"
 
            Using myConnection As New Data.SqlClient.SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New Data.SqlClient.SqlCommand(updateSql, myConnection)
                myCommand.Parameters.AddWithValue("@AspSessionId", AspSessionId)
                myCommand.Parameters.AddWithValue("@IpAddress", IpAddress)
                myCommand.Parameters.AddWithValue("@Http_Referer", HTTP_REFERER)
                myCommand.Parameters.AddWithValue("@pagefile", PageFile)
                myCommand.Parameters.AddWithValue("@UserId", UserId)
                myCommand.Parameters.AddWithValue("@script_name", SCRIPT_NAME)
                myCommand.Parameters.AddWithValue("@http_user_agent", HTTP_USER_AGENT)
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If
    End Sub
 
End Class

Open in new window

userList.txt
0
Comment
Question by:lotzaquestions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22773313
Hi,
You can add the code into a <script> tag in the aspx pages. E.g.


<head runat="server">
<title>Untitled Page</title>
<script type="text/VB">
       'VB Functions go here
</script>
</head>
0
 

Author Comment

by:lotzaquestions
ID: 22776526
thanks, M3mph15, I appreciate the prompt response.  When I structure the head as you say, the page executes without an error but the function doesn't seem to run.  I've reduced the head to a simplified version in the snippet - if I can get it to run I'll replace it with the database insert.
<%@ Page language="VB" Inherits="dotForumClientUsers.viewAllUsers" %>
 
<head  runat="server"> 
<title>Untitled Page</title> 
<script type="text/VB"> 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.write("testing script")
    End Sub
</script>
        <link href="gfx/forum/themes/fixed1024/fixed1024.css" rel="stylesheet" 
        type="text/css" />
</head>

Open in new window

0
 

Author Comment

by:lotzaquestions
ID: 22778868
ok, I fixed it.  However, suggested structure was:

<head runat="server">
<title>Untitled Page</title>
<script type="text/VB">
       'VB Functions go here
</script>
</head>

but this didn't work for me - the script didn't execute.   the actual structure that worked is:

<head>
<title>Untitled Page</title>
<script runat="server" language="VB">      
'VB Functions go here
</script>
</head>




0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 125 total points
ID: 22782902
Ah silly me. lol. Atleast you got it working now
0
 

Author Closing Comment

by:lotzaquestions
ID: 31508586
Your response put me on the right track.  thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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