[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

If I use a label the if loop works but if I use a variable it dosent. Why? A very simple question here for someone who knows how.

Posted on 2005-05-02
5
Medium Priority
?
169 Views
Last Modified: 2010-04-07
The following code works fine

If lbltest.Text = "" Then
            lbltest.Text = "ASC"
        ElseIf lbltest.Text = "ASC" Then
            lbltest.Text = "DESC"
        ElseIf lbltest.Text = "DESC" Then
            lbltest.Text = "ASC"
        End If

But if I try and use a variable to do the same thing it does not work.

eg

Dim SortDirection As String
        If SortDirection Is Nothing Then
            SortDirection = "ASC"
            lbltest.Text = SortDirection
        ElseIf SortDirection = "ASC" Then
            SortDirection = "DESC"
            lbltest.Text = SortDirection
        ElseIf SortDirection = "DESC" Then
            SortDirection = "ASC"
            lbltest.Text = SortDirection
        End If

According to my tests the lbltest.Text value goes to "ASC" and does not alternate. Why?

I have tried declaring SortDirection just after the web form class name and it still does not work. I cannot understand this since it works fine when just using the labels.

Im positive there is a very easy solution to this for someone who can spot the mistake.

Regards

Paul G
0
Comment
Question by:pgilfeather
  • 2
  • 2
5 Comments
 
LVL 13

Assisted Solution

by:softplus
softplus earned 600 total points
ID: 13908127
Hi Paul,
The problem is that the page is not "reused", i.e. when it's opened, the server doesn't know what happened the last time you opened the page. You can either use a global variable (e.g. use something like Session("SortDirection") to keep track on a per-session basis) or easier, use a hidden label control on the form.
John
0
 
LVL 18

Assisted Solution

by:tusharashah
tusharashah earned 200 total points
ID: 13908303
Declare your Variable SortDirection with Class declaration and as shared. What's happening here is your SortDirection is new everytime you call this routine. so value of SortDirection will be "ASC" everytime.

Shared SortDirection  As String = "ASC"

-tushar
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1200 total points
ID: 13908430
Do not store your sortdirection as shared. Because other instance of the same class may be running at the same time, and the sorting will be unpredictable for user. What you need to do is to store the sort order in view state (specific to the current Request pipeline)

Private Property SortDirection() As String
      Get
            Dim value As String = "ASC"
            If Not ViewState("SortDirection") Is Nothing Then
                  value = ViewState("SortDirection").ToString()
            End If
            Return value
      End Get
      Set (ByVal value As String)
            ViewState("SortDirection") = value
      End Set
End Property


Alternatively, you could use an enum
Private Property SortDirection() As SortDirectionList
      Get
            Dim value As SortDirectionList = SortDirectionList.Asc
            If Not ViewState("SortDirection") Is Nothing Then
                  value = DirectCast(ViewState("SortDirection"),SortDirectionList)
            End If
            Return value
      End Get
      Set (ByVal value As SortDirectionList)
            ViewState("SortDirection") = value
      End Set
End Property

Public Enum SortDirectionList
      Asc
      Desc
End Enum

Private Sub ToggleSortDirection()
      If SortDirection = SortDirectionList.Asc Then
            SortDirection = SortDirectionList.Desc
      Else
            SortDirection = SortDirectionList.Asc
      End If
End Sub

0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13908456
That's correct.. ty b1!
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13908508
No problems tusharashah,

to p,

By storing the simple values in the ViewState, they are available across postbacks specific to that request pipeline.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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