User Login session variable filtered

前端 未结 3 2096
感情败类
感情败类 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: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
    

提交回复
热议问题