2014年7月13日 星期日

[RESOLVED] use session in other page


Hi 


i have index page which is the first page that will appear for user to chosse his country and i have used this session for it:


Session["location"] = worlddrdolist.SelectedItem.Value;

Response.Redirect("Berava.aspx");


And in the Master page i have add a imagebtn and i want to use this imagebtn to allow user when he click on it to return him to the page Berava.aspx depend on the session 


protected void logobtn_Click(object sender, ImageClickEventArgs e)

{

Response.Redirect("Index.aspx");



}


Any help with it :-)



If you are storing a value within the Session in one area as seen below :


// You may want to consider using the SelectedValue property as SelectedItem may not always exist
Session["location"] = worlddrdolist.SelectedValue;

Then within another page (either another ASPX page or your Master Page), you'll want to check and ensure that your Session value exists and then handle it accordingly :


// Check if your Location value exists
if(Session["location"] != null)
{
// It exists, so determine where to navigate based on your values
string location = Session["location"];

switch(location)
{
case "LocationA":
// Navigate accordingly
break;
case "LocationB":
// Navigate accordingly
break;
default:
// Navigate accordingly
break;
}
}



.



Hi Rion William


Thanks for your reply, but i am using one page which is Berava.aspx only and inside this page i have ListView that show the items of each country depend on the session thats mean havent create many pages for many countries. So the user when he write the
website URL Index.aspx will appear and he have to chosse his country and when he click on movebtn the server will redirect him to Berava.aspx and the ListView will show the items of his country. So what i want if the user in any other page to redirect him
to Berava.aspx that apear to him after he chosse his country



So you basically would want to check if a location has been selected and if not direct the user to the Index page to allow him or her to select one. If you wanted to handle this application-wide, you might consider adding the following within your Master
Page (this assumes that all of the areas of your application will use your Master Page) :


// Check if your Location Session doesn't exist
if(Session["Location"] == null)
{
// Redirect the user to the Index.aspx page
Response.Redirect("Index.aspx");
}

After he / she has selected a country within the Index page, you can then redirect them to the appropriate page based on your selection. Then with another page loads, you can pull the value of your Session and use it to populate the items within your ListView.



沒有留言:

張貼留言