Solved

ASP.NET Session/ViewState Variables Button Control

Posted on 2009-03-31
11
580 Views
Last Modified: 2012-05-06
Hello,

I have successfully implemented an asp:button control.

In my code behind I have a boolean session variable called "IsLargeImage".  On Page Load, if the page isnt postback I add the session variable.

if (!IsPostback)
Session["IsLargeImage"] = false;

Then I have an if statement that checks what the value of the session variable is.

if (Session["IsLargeImage"])
     //Do something
else
    //Do something else

On the Button Click Event I simply check what the session value is then change it to the opposite value

if (Session["IsLargeImage"])
  Session["IsLargeImage"] = false;
else
  Session["IsLargeImage"] = true;

The code works okayish, but I have to click the button twice to finally get the value of the session variable to true.  For example.

The page loads, there is an image on the page and a button that says "Enlarge".  The image is default to small, so we click the "Enlarge" button, the page refreshes, but the image is still small, until we click the button again, the page refreshes and then the image is large.  From this point on it only takes 1 click to change from large to small and vice versa until we change to a totally new page.

I have tried session variables, viewstate variables, and form public variables and none of them see to be able to do the trick.

I am pretty new to ASP and ASP.NET, but I have several years experience in C#, so I can handle the code behind, im just not so good at the web development part. :o)  I am going to assume something like this is quite simple to change and I just dont have the knowledge on how to do it properly.

Anyway, anyone have any ideas?

Thanks
0
Comment
Question by:recruitit
  • 6
  • 5
11 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24027133
Can you post the content of your Page_Load please if this is where it is all happening, thanks
0
 

Author Comment

by:recruitit
ID: 24027202
The int values used in the SlideShowExtender are captured inside the Page Load but I didnt think the MySql statements would have been relevant to include.
protected void Page_Load(object sender, EventArgs e)

{

     if (!IsPostBack)

     {

          Context.Session["IsLargeImage"] = false;

     }
 

     if ((bool)Context.Session["IsLargeImage"])

     {

          SlideShowExtender1.ContextKey = string.Format("{0},{1},{2},1", intReqSecID, intReqDepID, intReqCatID);
 

          enlargeButton.Text = "Shrink";

     }

     else

     {

          SlideShowExtender1.ContextKey = string.Format("{0},{1},{2},0", intReqSecID, intReqDepID, intReqCatID);
 

          enlargeButton.Text = "Enlarge";

     }

}
 

public void enlargeButton_Click(object sender, EventArgs e)

    {

        if ((bool)ViewState["IsLargeImage"])

            Context.Session["IsLargeImage"] = false;

        else

            Context.Session["IsLargeImage"] = true;

    }

Open in new window

0
 

Author Comment

by:recruitit
ID: 24027217
The web site is public, although not populated by google yet if you wanted to check it out.

I am using an AJAX SlideShowExtender to cycle through images in the website.  Although completely different situation, I am also trying to load the image URL into the text box so I can parse it and know which page I need to send the user to when they click the view details button.

http://www.motosportimages.com/store/prods.aspx?t=6&d=18&c=61 
0
 
LVL 23

Expert Comment

by:apresto
ID: 24027228
Try saving the session variable to a local variable and accessing it that way:
bool isLarge = Session["IsLargeImage"];
if ( isLarge )
{
    // ...
}
It may also be worth putting this code in your page_init event:
if (!IsPostBack)
     {
          Context.Session["IsLargeImage"] = false;
     }
 
 
0
 

Author Comment

by:recruitit
ID: 24027292
Thanks for that,  unfortionetly it hasn't cured it.

To be honest it doesnt have to be a session variable or anything, I just need a value I can track on this page.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:recruitit
ID: 24040231
Nice to know people like to solve questions.......
0
 
LVL 23

Expert Comment

by:apresto
ID: 24040246
Very sorry, i read this and forget to set my mail as unread. Ill have a look at it now and post back. Thanks for the reminder.
You'll notice my profile does state to remind me if i forget, i know you shouldnt have to, but i participate in a number of questions every day.
will post back soon
0
 
LVL 23

Expert Comment

by:apresto
ID: 24040292
Ok, quick suggestion, remove the check code from your pageload and add it to the page_init event instead: (you may not already have thie event, so add it if you dont already have it)
void Page_Init(object sender, EventArgs e)

{

  if (!IsPostback)

  {

     Session["IsLargeImage"] = false;

  }

}

Open in new window

0
 

Author Comment

by:recruitit
ID: 24040887
No problem, I should know to do this myself to be honest so im not blaming anyone.

Ill try what you suggested, thanks.
0
 

Author Comment

by:recruitit
ID: 24057412
I cant seem to find this event, is it an override?  Cant seem to find it.
0
 
LVL 23

Accepted Solution

by:
apresto earned 250 total points
ID: 24057661
Just paste the above into your page codebehind. the Page_Init fires before the Page_Load. Set a breakpoint inside it and you will see that it gets fired before anything else when you run your page
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to page 4 74
Windows 10 IIS and Windows Authentication to SQL 21 54
error on the asp.net page 1 33
Interview questions for support of a legacy ASP.NET site 4 39
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now