User Login session variable filtered

前端 未结 3 2094
感情败类
感情败类 2021-01-28 04:47

I am trying to compare a session variable to another string so that i can enable or disable image buttons. I am using asp.net vb with a sql2005 express backend

P         


        
相关标签:
3条回答
  • 2021-01-28 05:10

    String Compare returns 0 if the strings are equal. To evaluate for true change your comparison to = 0 rather than <> 0.

    If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) = 0 Then
    

    If you envision performing such checks frequently to display different portions of a page you might want to break your pages up based on the different types (or levels) of users you expect and redirect them to a particular page upon login. Depending on how similar the pages are between users you'll likely find using master pages and/or user controls to be beneficial.

    0 讨论(0)
  • 2021-01-28 05:14

    this is works!

        ImageButton1.Enabled = Not Session("UserName") Is Nothing AndAlso Session("UserName").ToString.ToLower.Trim.Equals("username")
    

    ImageButton2.Enabled = Not Session("UserName") Is Nothing AndAlso_ Session("UserName").ToString.ToLower.Trim.Equals("username")

    0 讨论(0)
  • 2021-01-28 05:20

    String.Compare returns -1, 0 or 1 depending on the result:

    String.Compare("A", "B", true) ' returns -1 '
    String.Compare("B", "A", true) ' returns 1 '
    String.Compare("B", "B", true) ' returns 0 '
    

    In your case, ImageButton1 will be disabled if the user name would come before "medev" if you sorted them as a list. So "Adam" wound have it disabled, while "Stuart" would have it enabled. "medev" would also get it disabled, because if the strings are equal, String.Compare returns 0. My guess is that you want ImageList1 enabled for "medev":

    If String.Compare(string1, string2, True) <> 0 Then
        ImageButton1.Enabled = False
    End If
    

    I would also suggest using the String.Compare overload that takes a StringComparison instead of a bool to get a case insensitive compare. In my opinion it makes it a lot easier to understand what the code does:

    If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) <> 0 Then
        ImageButton1.Enabled = False
    End If
    
    0 讨论(0)
提交回复
热议问题