2014-09-22 73 views
0

你好傢伙我正在爲我的最後一年項目製作一個小型asp.net項目, 我正在製作一個在線購物網站。 我有三個頁面在我的網站Login.aspx,detail.aspx和mobile.aspx 在mobile.aspx當用戶點擊詳細按鈕時,它將用戶重定向到detail.aspx頁面 使用response.redirect();(no問題在這一步)。 現在在detail.aspx頁面中,當用戶點擊addtoCart按鈕時,頁面首先檢查用戶是否登錄(使用session [「authenticated」]!= null方法) 如果它已經登錄,那麼每件事情都會發生根據我的問題 現在問題來了,當用戶沒有登錄,並點擊addtoCart按鈕,它將用戶重定向到login.aspx頁面 在登錄頁面當用戶點擊登錄按鈕時,它會檢查用戶是否從詳細信息頁面重定向(使用會話[「的productID」]!= NULL) 如果是,應該將用戶重定向回detail.aspx頁,但它沒有這樣做,這是我的代碼,請幫我在asp.net中成功登錄之後將用戶重定向到源頁面

login.aspx的

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    SqlConnection connection = new SqlConnection(conn); 
    connection.Open(); 
    cmd = new SqlCommand("select COUNT(*) from customer_login where login_id = @a and [email protected]",connection); 
    cmd.Parameters.AddWithValue("@a", Login1.UserName); 
    cmd.Parameters.AddWithValue("@b", Login1.Password); 
    string user_name; 
    int i = Convert.ToInt32(cmd.ExecuteScalar().ToString()); 

    ViewState["PreviousPage"] = Request.UrlReferrer.ToString();//to retrive the url from where it is redirected, this will be used to redirect the user from where it comes(detail.aspx) 

    if (i == 1) 
    { 
     e.Authenticated = true; 

     Session["authenticatedUser"] = Session.SessionID; 
     Session["loginid"] = Login1.UserName.ToString(); 
     cmd = new SqlCommand("select f_name from customer where id = (select cust_id from customer_login where login_id = @a)", connection); 
     cmd.Parameters.AddWithValue("@a", Login1.UserName); 
     sdr = cmd.ExecuteReader(); 
     sdr.Read(); 
     user_name = sdr["f_name"].ToString(); 
     sdr.Close(); 

     if (Session["productID"] != null&&ViewState["PreviousPage"].ToString())//to check if the user is redirected from detail.aspx page 
     { 
      Session["user"] = user_name.ToString(); 
      Response.Redirect(ViewState["PreviousPage"].ToString()); 
     } 
     else 
     { 
      Session["user"] = user_name.ToString(); 
      Response.Redirect("Index.aspx"); 
     } 
    } 
    else 
    { 
     e.Authenticated = false; 
    } 
} 

登錄的結果是不是將用戶重定向到的Index.aspx頁的詳細信息頁面

回答

0

我發現我用在detail.aspx頁面查詢字符串的解決方案發送源網頁的網址(detail.aspx)

  Response.Redirect("~/login.aspx?redirect="+Request.Url.ToString()); 

,然後檢索入login.aspx的

 if (Session["productID"] != null&&!string.IsNullOrEmpty(Request.QueryString["redirect"])) 
     { 
      Session["user"] = user_name.ToString(); 
      Response.Redirect(Request.QueryString["redirect"].ToString()+"&ssid="+Session["authenticatedUser"].ToString()); 
     //Response.Redirect(ViewState["PreviousPage"].ToString()); 
     // Response.Redirect("~/shop/Cart.aspx?ssid="+Session["authenticatedUser"].ToString()+"&pr="+Session["productID"].ToString()); 
     } 
     else 
     { 
      Session["user"] = user_name.ToString(); 
      Response.Redirect("Default.aspx"); 
     } 
相關問題