Global.asax and Events

Last post 08-20-2009 8:21 AM by sirdneo. 5 replies.

Sort Posts:

  • Global.asax and Events

    08-19-2009, 11:36 AM
    • Member
      point Member
    • papaklump
    • Member since 08-19-2009, 3:31 PM
    • Posts 2

    I have a quick question about the Global.asax file and its events. (im a CF programmer by trade and very familiar with the application.cfc)

    I am trying to capture the clients IP and write it to table on on every page load. What event would i use in global.asax? I have tried Application_RequestStart and it seems to add more than one record to the db on one page visit. The code should only add one record to the table on each page request. Am i using the correct event? Do i need to take into account certain scenarios? Can someone throw some help my way?

    Thanks!

  • Re: Global.asax and Events

    08-19-2009, 11:55 AM
    • Contributor
      2,386 point Contributor
    • lohith.bn
    • Member since 08-03-2009, 10:14 AM
    • Bangalore
    • Posts 403

    better use in your page load event

    C#

    string str = Page.Request.UserHostAddress;

    string str1 = Page.Request.ServerVariables["REMOTE_ADDR"];

    VB.Net

    Dim str As String = Request.UserHostAddress

    Dim str2 As String = Request.ServerVariables("REMOTE_ADDR")

     

    or

    check this link

    http://bdotnet.in/blogs/lohithbn/archive/2007/12/31/session-state-in-global-aspx-file.aspx

    lohith B N
    My Blog Link
    Dont forget to click "Mark as answer" on the post that helped you.
  • Re: Global.asax and Events

    08-19-2009, 12:11 PM
    • Member
      point Member
    • papaklump
    • Member since 08-19-2009, 3:31 PM
    • Posts 2

    Is there a page load event in global.asax?

  • Re: Global.asax and Events

    08-19-2009, 2:18 PM
    Answer
    • All-Star
      25,062 point All-Star
    • budugu
    • Member since 01-12-2006, 2:15 PM
    • North Carolina
    • Posts 3,750

    papaklump:
    Is there a page load event in global.asax?
     

    None.

    papaklump:
    I am trying to capture the clients IP and write it to table on on every page load. What event would i use in global.asax? I have tried Application_RequestStart and it seems to add more than one record to the db on one page visit. The code should only add one record to the table on each page request. Am i using the correct event? Do i need to take into account certain scenarios? Can someone throw some help my way?

    Check this article..

    http://dotnetslackers.com/articles/aspnet/Tracking-User-Activity.aspx

     

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: Global.asax and Events

    08-20-2009, 6:02 AM

    i think Application_RequestStart() event is calling everytime when the request for the page is coming.. May be That's the reason it is creating multiple entries

    Try to debug this event and test you application..You may find out the reason y it is coming..

    I think the best way is to write this code in page_load event which was already suggested by "lohith B N"

    Please click "Mark as Answer" if you think this post answer your question

    Best Regards,
    Suresh Kumar Gundala
  • Re: Global.asax and Events

    08-20-2009, 8:21 AM
    Answer
    • Contributor
      7,268 point Contributor
    • sirdneo
    • Member since 12-16-2008, 5:45 AM
    • Karachi, Pakistan
    • Posts 1,144

     You can create a base page, in wich you get the IP information and save it to DB,, and drive all your pages from that pages like this:-

     

    public class BasePage : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)//Check that it is not a postback request to avoid duplication
                {
                    string str1 = Page.Request.ServerVariables["REMOTE_ADDR"];
                    //Save if to DB
    
                }
            }}
    
    


     

    and then drive all your pages from that page. like this:-

     

    public partial class MyPage: BasePage

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
Page 1 of 1 (6 items)