SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Simple button action

I'm using the code below to determine whether or not the page should show images or not. The button "works" sometimes, but other times it will take 2 or 3 clicks in order for the images to display.

Am I missing something?

    Private Sub btnViewAll_Click(sender As Object, e As System.EventArgs) Handles btnViewAll.Click
    If Session("fullPage") IsNot Nothing Then
        If CInt(Session("fullPage")) = 0 Then
            Session("fullPage") = 1
        Else
            Session("fullPage") = 0
        End If
    Else
        Session("fullPage") = 1
    End If
End Sub

I check for the session here:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvFolders.RowDataBound

    If CInt(Session("fullPage")) = 1 Then
      ...load images....
    End If

End Sub

Upvotes: 1

Views: 173

Answers (1)

JDB
JDB

Reputation: 25810

My guess is that you are setting your Session variable after you have referenced it.

For example, if you are using the variable in the Page_Load, then on the first click the value will be False by default (causing you to not render the images). After the Page_Load event has finished, the click event handler will fire, setting the session variable to True. But, since Page_Load has already finished, you will not see the images until after Page_Load is called again (on the second postback).

This is only one example of what can happen when you are not paying close attention to the page life cycle. Since we can't see your code, it's impossible to know exactly where the logic error is occurring.

See the ASP.NET Page Life Cycle for more information about the order of events.


EDIT
You may need to move some of your logic from the Page_Load to the Page_LoadComplete event. LoadComplete is usually the best place to do data binding, etc, after you have processed your control events.

To answer your question in the comments below, the session handling is actually in the appropriate place. I can't see where you are calling GridView1.DataBind(), but most likely it is that code which is not in the correct place. Move the DataBind call to the LoadComplete page event, and you should be set.

Upvotes: 5

Related Questions