开发者

Pages requires two loads to change string

I have the following code which seems to work OK except for a minor problem.

When the user first visits the page the correct phone number appears but on the second visit - if they should see a different number, they need to load the page twice before the number changes to the new one.

I'm not too sure how to explain this best so if you need more info please do ask.

  Public freeCallNumber As String

Private Function getFreeCallNumber(ByVal value As String) As String
    Select Case value
        Case "EZ12" 
            Return "0800 11 22 333"
        Case "WT45" 
            Return "0800 44 55 666"
        Case Else
            Return "0800 77 88 999"
    End Se开发者_高级运维lect      
End Function    

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

    'set cookies here'

    If Not Request.Cookies("LatestRefer") is Nothing Then
        freeCallnumber = getFreeCallNumber(Request.Cookies("LatestRefer").Value)
    Else
        freeCallnumber = getFreeCallNumber(Request.Cookies("FirstRefer").Value)
    End If          

End Sub


It's probably because the cookies aren't set until the next page request. Cookies are sent as "SET-COOKIE"-headers to the client in the response. That means that when you later on in your code do Request.Cookies the client haven't sent them to the page yet. That happens on the next request from the user.

Thus, Response.Cookies is not the same as Request.Cookies.

What you can do is to determine what cookies you want to set, save in the a private variable and then set cookies as normal to the client. Then, later on in your code you check the variable instead of Request.Cookies.

Private cookieReferer as String

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

    cookieReferer = "defautlValue"
    // Add cookie with value from 'cookieReferer'

End Sub


    freeCallnumber = getFreeCallNumber(cookieReferer)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜