Link to home
Start Free TrialLog in
Avatar of dpmoney
dpmoneyFlag for United States of America

asked on

ASP.NET GridView Hyperlink Field Change URL Font Color in Code Behind (VB)

I have a VB ASP.NET GridView with the first column as a Hyperlink Field.  
Need to change font color of hyperlink text from standard blue to white in row databound event for just orders that are closed.
I'm already successfully changing cell background color to red in an If-Then statement.
Just need to change URL text to white.  Having issues since the URL field is treated differently.

Here is the code block and where I need to change the color:

'Order Status - Open
            If e.Row.Cells(15).Text.ToString = "False" Then
                e.Row.Cells(15).Text = "Open"
                        e.Row.Cells(4).BackColor = drawing.color.darkseagreen
            Else
                'Order Status - Closed
                e.Row.Cells(15).Text = "Closed"
                        e.Row.BackColor = Drawing.Color.Firebrick
                        
[CHANGE URL TEXT COLOR WHITE HERE]
                        
                  End If

I can put in a line of code like this for the first column, but it does NOT change the color of the text because it is a hyperlink - has no effect:

                        e.Row.Cells(0).ForeColor = drawing.color.white

If I change the zero to a 1 or 2, it changes color of other cells fine.  The hyperlink field is causing the issue.

THANKS!
GridViewHyperLinkFontColor.png
Avatar of balagangadhar T
balagangadhar T

For this you have to create a  css class as below
a:link {
    color: red;
}

/* visited link */
a:visited {
    color: green;
}

/* mouse over link */
a:hover {
    color: hotpink;
}

/* selected link */
a:active {
    color: blue;
}
Then in row data bound event for your code do the following
   e.Row.Cells(15).Text = "Closed"
                        e.Row.FindControl(controlid).cssclass = "a:link"

If you want to change the color then just mention the color in a:link class
Avatar of dpmoney

ASKER

I played around with this over the weekend and also came up with a CSS solution.  However, it did the same thing you code snippet is doing....applying the white hyperlink font to EVERY row in the grid view.   Even the rows that are designated as OPEN vs. CLOSED.  That is strange since we are doing the CSS assignment in the If|Then block where it does one thing vs. another based on whether order is open or closed.  Can you think of why it is applying the white font to every hyperlink vs. just the CLOSED rows?
ASKER CERTIFIED SOLUTION
Avatar of dpmoney
dpmoney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is good that you figured out. But you never mentioned that you are not using the hyperlink. I assumed that you are using the hyperlink and that is why i have mentioned the code e.Row.FindControl(controlid).
Avatar of dpmoney

ASKER

The only solution provided by a community member did not work as required.  

Further research allowed me to figure out a solution on my own.