2011-06-11 45 views
1

我是ASP.NET的新用戶,因此請耐心等待:D 我想將其中一個頁面重定向到另一個頁面,並保留用戶名! 我試圖使用session.add和會話[],但是當我想插入括號內的用戶名,它說使用必須int!但我想我應該使用會話[「用戶名」] 我用另一種方式(的Request.QueryString []),但兩者都有問題 這裏是我的代碼使用會話重定向頁面

//first solution  
    string username="asal"; 
    session.Add(username,username); 
    Response.Redirect("~/Doctor/DoctorsMainPage.aspx"); 
    //in the other page 
    Label1.Text= Session["username"].ToString();//this one says use int?! 
    //i used this one instead of it 
    Label1.Text= Session[0].ToString();//with this one i get the username in other page,but one i want to pass another string like "id" with session,I can not! 

    //the second solution 
    string username="asal"; 
    Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username"); 
    Label1.Text = Request.QueryString["username"];//this one redirect to doctors main page but set the value of username to "" ! 

回答

0

您必須添加會話如..

session.Add("username",username);代替session.Add(username,username);

然後你就可以訪問值如.. Label1.Text= (String)Session["username"];

查看這篇與會話狀態ASP.NET Session State Overview相關的文章,這將幫助您瞭解會話狀態管理。

Seconly查詢字符串應該是這樣,因爲你還沒有通過你的字符串參數,它應該是這樣......

Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username=" + username); 
0

session.Add(字符串,字符串),其中第一個字符串的名字變量和第二個是值。

您正在兩次添加值。

//first solution  
string username="asal"; 
session.Add("username",username); <-- this is your problem 
Response.Redirect("~/Doctor/DoctorsMainPage.aspx"); 

//in the other page 
Label1.Text= Session["username"].ToString(); 

現在,作爲

//the second solution 
string username="asal"; 
Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username"); 
Label1.Text = Request.QueryString["username"];//this one redirect to doctors main page but set the value of username to "" ! 

在這種情況下,你要創建一個URL 「〜/醫生/ DoctorsMainPage.aspx用戶名?」

好了 - 那麼,什麼是用戶名?代碼正在尋找名爲username的查詢字符串中的param,但它沒有找到值。 您需要:

Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username="+username); 

這將會給你? 「〜/醫生/ DoctorsMainPage.aspx用戶名=荒漠和半荒漠」

+0

哦TNX了很多! :) – asal 2011-06-11 10:24:09

0
string username = "asal"; 
     Session["username"] = username; 
     Response.Redirect("~/Doctor/DoctorsMainPage.aspx"); 

     //Other page 
     Label1.Text = Session["username"].ToString().Trim(); 
相關問題