2

I try to create a web application that got a button that change an image. this is my code:

public partial class _Default : System.Web.UI.Page
{
    private bool test ;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (test)
        {
            Image1.ImageUrl = @"~/images/il_570xN.183385863.jpg";
            test = false;
        }else
        {
            Image1.ImageUrl = @"~/images/BAG.png";
            test = true;
        }

    }
}

my problem is that the page reload every time. meaning, after i click the button "test" return to it's initial value. how can i have a variable that i can access all through the session?
please notice, i don't want to solve this specific image problem, but to know how to keep data until the user closed the page.

0

6 Answers 6

7

You can store arbitrary values in Session

Session["someKey1"] = "My Special Value";
Session["someKey2"] = 34;

Or more complex values:

Session["myObjKey"] = new MyAwesomeObject();

And to get them back out:

var myStr = Session["someKey1"] as String;
var myInt = Session["someKey2"] as Int32?;
var myObj = Session["myObjKey"] as MyAwesomeObject;
Sign up to request clarification or add additional context in comments.

Comments

2

ASP.NET webforms are stateless so this is by design.

you can store your bool variable in the ViewState of the page so you will always have it updated and persisted within the same page.

Session would also work but I would put this local page related variable in the ViewState as it will be used only in this page ( I guess )

2 Comments

The user specifically stated that the example above was not what he was trying to solve... rather how to store values until the user closed the browser. By definition that is Session
yes you are right but from the code what he needs is clear and I told the difference in my answer :)
1

Store the variable in a cookie! :)

Example:

var yourCookie = new HttpCookie("test", true);
Response.Cookies.Add(yourCookie);

1 Comment

I didn't say it is the perfect solution, he asked for A solution.. :)
1
Session["show_image"] = "true";

1 Comment

bad idea to store in the session variables used only in 1 page!
0

To achieve what you want you need to check if is PostBack or not as so:

protected void Button1_Click(object sender, EventArgs e)
{

    if (test && !IsPostBack)
    {
        Image1.ImageUrl = @"~/images/il_570xN.183385863.jpg";
        test = false;
    }else
    {
        Image1.ImageUrl = @"~/images/BAG.png";
        test = true;
    }

}

BUT, don't do it that way. You are approaching this wrong. You wouldn't want to store in Session things like whether this image is shown or not, etc. It's difficult to suggest an approach without knowing the specific issue you are facing.

Comments

0

Another option than the already mentioned would be to store the data in the ViewState. If it's just used in postback situations, that might be a possible way.

ViewState["test"] = true;

You save memory on the server but use a little bit of bandwith. The data gets lost, when the user browses to another page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.