Wednesday, March 21, 2012

Passing A String

Hello, I am a newbie. Thanks for all the great help I have recieved so far. Here is my latestest question.
I am trying to capture the referring site and the landing page url with query string on the initial entry page of my website. I am able to add these values to session, but they get overwritten each page I visit. My guess is that I need to write some sort of boolean test so these values are not added again if they already exist. Here is the code I have written.
Thanks for your help.
John

// Get query string

string urlreffer =Convert.ToString(Request.UrlReferrer);

string urlraw =Convert.ToString(Request.RawUrl);

string sessionid =Convert.ToString(Request.Cookies);

// add to session

Session.Add("reffer", urlreffer);

Session.Add("rawurl", urlraw);

Session.Add("sessionid", sessionid);

Hello,
In what event do you have this code, in the page_Load event? If so, surround the code with:
if (!page.IsPostBack) {
}
If another event, let me know.
HTH.

protectedvoid Page_Load(object sender,EventArgs e)

{

// Get query string

string urlrefer =Convert.ToString(Request.UrlReferrer);

string urlraw =Convert.ToString(Request.Url);

string sessionid =Convert.ToString(Request.Cookies["ASP.NET_SessionId"].Value);

// test to see if values are already in session.

if (Session["refer"] ==null ||Convert.ToString(Session["refer"]).Length == 0)

{

// add to session

Session.Add("refer", urlrefer);

Session.Add("rawurl", urlraw);

Session.Add("sessionid", sessionid);

}

}

}

0 comments:

Post a Comment