asp.net,如何在VB代码的子节点中将文本颜色更改为红色?

时间:2013-11-04 15:25:37

标签: asp.net vb.net

我现在通过服务器名列表获得了所有工作,但是我想通过检查SQL Server中名为Compliance的列来添加IF语句,列出的是True或False值。如果为False,则Name将文本颜色更改为Red。如果为True,则名称不会更改文本颜色。我不确定如何在VB代码中添加它。我很确定我需要将IF语句放在 while dr.Read()中。我是VB.Net的新手,不知道改变文本颜色的VB代码。

这是我的VB代码,

Sub loadData()
    'clear treeview control
    TreeViewGroups.Nodes.Clear()

    'fetch owner data and save to in memory table
    Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
    Dim strSqlSecondary As String = "SELECT [Name] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    'Getting a list of True or False from Compliance column
    Dim strSqlCompliance As String = "SELECT [Compliance] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    Dim cmdSecondary As New System.Data.SqlClient.SqlCommand(strSqlSecondary, sqlConn)

    Dim cmdCompliance As New System.Data.SqlClient.SqlCommand(strSqlCompliance, sqlConn)

    cmdSecondary.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())

    cmdCompliance.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())

    Dim dr As System.Data.SqlClient.SqlDataReader
    Try
        sqlConn.Open()
        Dim root As TreeNode
        Dim rootNode As TreeNode
        Dim firstNode As Integer = 0
        'Load Primary Owner Node
        'Create RootTreeNode

        dr = cmdSecondary.ExecuteReader()
        If dr.HasRows Then
            'Load Secondary Owner Node
            'Create RootTreeNode
            root = New TreeNode("Secondary Owner", "Secondary Owner")
            TreeViewGroups.Nodes.Add(root)
            root.SelectAction = TreeNodeSelectAction.None

            rootNode = TreeViewGroups.Nodes(firstNode)
            'populate the child nodes
            While dr.Read()
                Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))
                rootNode.ChildNodes.Add(child)
                child.SelectAction = TreeNodeSelectAction.None
            End While
            dr.Close()
            cmdSecondary.Dispose()
        End If

        'check if treeview has nodes
        If TreeViewGroups.Nodes.Count = 0 Then
            noServers()
        End If
    Catch ex As Exception
        hide()
        PanelError.Visible = True
        LabelError.Text = ex.ToString()
    Finally
        sqlConn.Dispose()
    End Try
End Sub

2 个答案:

答案 0 :(得分:1)

您需要检查合规性值是否可以转换为布尔值,然后将DIV环绕文本并使用红色样式,如下所示:

While dr.Read()
    Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))

    ' Test whether compliance value can be converted to Boolean type or not
    Dim complianceFlag As Boolean 

    If Boolean.TryParse(dr("Compliance"), complianceFlag) Then
        ' Yes, compliance value is a Boolean, now set color based on value
        If Not complianceFlag Then
            child.Text = "<div style='color:Red; float: left;'>" + child.Text & "&lt;/div>"
        End If
    Else
        ' Unable to convert compliance value to Boolean
        ' Do something here if you want or just ignore it
    End If

    rootNode.ChildNodes.Add(child)
    child.SelectAction = TreeNodeSelectAction.None
End While

注意:如果预期的转换失败,TryParse将不会抛出异常,因此需要complianceFlag的输出变量。

答案 1 :(得分:0)

我得到了代码工作

Sub loadData()
    'clear treeview control
    TreeViewGroups.Nodes.Clear()

    'fetch owner data and save to in memory table
    Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
    Dim strSqlSecondary As String = "SELECT [Name], [Compliance] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    Dim cmdSecondary As New System.Data.SqlClient.SqlCommand(strSqlSecondary, sqlConn)


    cmdSecondary.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())


    Dim dr As System.Data.SqlClient.SqlDataReader


    Try
        sqlConn.Open()
        Dim root As TreeNode
        Dim rootNode As TreeNode
        Dim firstNode As Integer = 0
        'Load Secondary Owner Node
        'Create RootTreeNode    
        dr = cmdSecondary.ExecuteReader()

        If dr.HasRows Then
            'Load Secondary Owner Node
            'Create RootTreeNode
            root = New TreeNode("Secondary Owner", "Secondary Owner")
            TreeViewGroups.Nodes.Add(root)
            root.SelectAction = TreeNodeSelectAction.None

            rootNode = TreeViewGroups.Nodes(firstNode)
            'populate the child nodes
            While dr.Read()
                Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))
                Dim complianceFlag As Boolean

                If Boolean.TryParse(dr("Compliance"), complianceFlag) Then
                    ' Yes, compliance value is a Boolean, now set color based on value
                    If Not complianceFlag Then
                        child.Text = "<div style='color:Red'>" + child.Text & "</div>"
                    End If
                Else

                End If
                rootNode.ChildNodes.Add(child)
                child.SelectAction = TreeNodeSelectAction.None
            End While
            dr.Close()

            cmdSecondary.Dispose()

        End If


        'check if treeview has nodes
        If TreeViewGroups.Nodes.Count = 0 Then
            noServers()
        End If

    Catch ex As Exception
        hide()
        PanelError.Visible = True
        LabelError.Text = ex.ToString()
    Finally
        sqlConn.Dispose()
    End Try
End Sub