在下面的 ASP.Net 4.5 Gridview Databound 事件中,它工作得很好,在我看来,“If Not usr Is Nothing Then”代码行实际上应该在 For EAch DataControlField 循环之外,因为我只需要检查一次。但是,当我用“If Not usr Is Nothing Then”(请参阅第二个代码迭代)包装整个逻辑时,我得到一个错误。有什么想法吗
Protected Sub gvEBEvals_DataBound(sender As Object, e As EventArgs) Handles gvEBEvals.DataBound
Dim revColumn As Integer = -1
Dim currentColumn As Integer = -1
Dim usr As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
For Each col As DataControlField In gvEBEvals.Columns
If Not usr Is Nothing Then
If (Roles.IsUserInRole(usr.UserName, "administrator") Or Roles.IsUserInRole(usr.UserName, "developer")) Then
If col.HeaderText.ToLower().Trim() = "revenue potential" Then
revColumn = gvEBEvals.Columns.IndexOf(col)
For Each row As GridViewRow In sender.rows
If revColumn > -1 Then
row.Cells(revColumn).Text = String.Format("{0:c0}", Convert.ToDouble(row.Cells(revColumn).Text))
End If
Next
End If
End If
End If
If col.HeaderText.ToLower().Trim() = "is current" Then
currentColumn = gvEBEvals.Columns.IndexOf(col)
For Each row As GridViewRow In sender.Rows
If currentColumn <> -1 Then
If row.Cells(currentColumn).Text = "True" Then row.Font.Bold = True
End If
Next
End If
Next
End Sub
修改代码使用一次调用测试用户用户的存在,哪些错误
If Not usr Is Nothing Then
For Each col As DataControlField In gvEBEvals.Columns
If (Roles.IsUserInRole(usr.UserName, "administrator") Or Roles.IsUserInRole(usr.UserName, "developer")) Then
If col.HeaderText.ToLower().Trim() = "revenue potential" Then
revColumn = gvEBEvals.Columns.IndexOf(col)
For Each row As GridViewRow In sender.rows
If revColumn > -1 Then
row.Cells(revColumn).Text = String.Format("{0:c0}", Convert.ToDouble(row.Cells(revColumn).Text))
End If
Next
End If
End If
End If
If col.HeaderText.ToLower().Trim() = "is current" Then
currentColumn = gvEBEvals.Columns.IndexOf(col)
For Each row As GridViewRow In sender.Rows
If currentColumn <> -1 Then
If row.Cells(currentColumn).Text = "True" Then row.Font.Bold = True
End If
Next
End If
Next
Else
For Each col As DataControlField In gvEBEvals.Columns
If col.HeaderText.ToLower().Trim() = "is current" Then
currentColumn = gvEBEvals.Columns.IndexOf(col)
For Each row As GridViewRow In sender.Rows
If currentColumn <> -1 Then
If row.Cells(currentColumn).Text = "True" Then row.Font.Bold = True
End If
Next
End If
Next
End If