Hyperlink Format

C

chuckdfoster

I have a hyperlink column in my datagrid. I want my hyperlink to be green
or red (depending on in/out status) no matter if the link has been visited
or not. How can I accomplish this? I have tried using CSS but that doesn't
affect what is going on in my datagrid. This is what I have so far for
formatting in my datagrid...

Sub ItemBound(ByVal sender As Object, ByVal e as DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
'check inout field, if In then font is green, if Out then font is
red
Dim strInOut as String = DataBinder.Eval(e.Item.DataItem, "inout")
If strInOut = "In" then
e.Item.ForeColor = System.Drawing.Color.Green
Else
e.Item.ForeColor = System.Drawing.Color.Red
End If
End If
End Sub

Any advice is greatly appreciated....Thanks in advance!
 
K

Ken Cox [Microsoft MVP]

Hi Chuck,

I think your problem with CSS is that you're trying to change the class of
the whole row and not just the hyperlink.

Here's some sample code that seems to do what you want. It changes the
Cssclass attribute of the hyperlink according to the inbound value.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>dghlnk</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<style>.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="In/Out">
<itemtemplate>
<asp:HyperLink ID="myhlink" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.inout") %>' NavigateUrl='<%#
"http://www.aspalliance.com?id=" & DataBinder.Eval(Container,
"DataItem.Integervalue") %>' Target="_blank">
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim strInOut As String = DataBinder.Eval(e.Item.DataItem,
"inout")
hlnk = e.Item.FindControl("myhlink")
If strInOut = "In" Then
hlnk.CssClass = "inclass"
Else
hlnk.CssClass = "outclass"
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("inout", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
If (i Mod 2) = 0 Then
dr(1) = "In"
Else
dr(1) = "Out"
End If
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
C

chuckdfoster

Thank you so much for the help, but I'm still having a little problem.

I understand what you mean about changing the whole row and not just the
hyperlink. I actually want to change the whole, including the hyperlink.
It changes the entire row except for the hyperlink. I'm a little confused
about how the .inclass and .outclass works in the CSS part. I added it to
my page, but no change. I figure that I wasn't supposed to put .inclass and
..outclass,but I don't know what to put in its place. Can you help just a
little more?
<style>
.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>

Thanks again!
--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System

Ken Cox said:
Hi Chuck,

I think your problem with CSS is that you're trying to change the class of
the whole row and not just the hyperlink.

Here's some sample code that seems to do what you want. It changes the
Cssclass attribute of the hyperlink according to the inbound value.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>dghlnk</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<style>.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="In/Out">
<itemtemplate>
<asp:HyperLink ID="myhlink" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.inout") %>' NavigateUrl='<%#
"http://www.aspalliance.com?id=" & DataBinder.Eval(Container,
"DataItem.Integervalue") %>' Target="_blank">
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim strInOut As String = DataBinder.Eval(e.Item.DataItem,
"inout")
hlnk = e.Item.FindControl("myhlink")
If strInOut = "In" Then
hlnk.CssClass = "inclass"
Else
hlnk.CssClass = "outclass"
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("inout", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
If (i Mod 2) = 0 Then
dr(1) = "In"
Else
dr(1) = "Out"
End If
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


chuckdfoster said:
I have a hyperlink column in my datagrid. I want my hyperlink to be green
or red (depending on in/out status) no matter if the link has been visited
or not. How can I accomplish this? I have tried using CSS but that
doesn't
affect what is going on in my datagrid. This is what I have so far for
formatting in my datagrid...

Sub ItemBound(ByVal sender As Object, ByVal e as DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
'check inout field, if In then font is green, if Out then font is
red
Dim strInOut as String = DataBinder.Eval(e.Item.DataItem, "inout")
If strInOut = "In" then
e.Item.ForeColor = System.Drawing.Color.Green
Else
e.Item.ForeColor = System.Drawing.Color.Red
End If
End If
End Sub

Any advice is greatly appreciated....Thanks in advance!
 
K

Ken Cox [Microsoft MVP]

Hi Chuck,

Not exactly sure what you mean, but you do have to treat the row and the
hyperlink separately as far as assign the style class goes. Here, I assign
different classes to the hyperlink and the row. Maybe this is what you're
after?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>dghlnk</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<style>
.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
.inItemclass {
COLOR: green;
}
.outItemclass {
COLOR: red;
}
</style>
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="In/Out">
<itemtemplate>
<asp:HyperLink ID="myhlink" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.inout") %>' NavigateUrl='<%#
"http://www.aspalliance.com?id=" & DataBinder.Eval(Container,
"DataItem.Integervalue") %>' Target="_blank">
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim strInOut As String = DataBinder.Eval(e.Item.DataItem,
"inout")
hlnk = e.Item.FindControl("myhlink")
If strInOut = "In" Then
hlnk.CssClass = "inclass"
e.Item.CssClass = "inItemClass"
Else
hlnk.CssClass = "outclass"
e.Item.CssClass = "outItemClass"
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("inout", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
If (i Mod 2) = 0 Then
dr(1) = "In"
Else
dr(1) = "Out"
End If
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource



chuckdfoster said:
Thank you so much for the help, but I'm still having a little problem.

I understand what you mean about changing the whole row and not just the
hyperlink. I actually want to change the whole, including the hyperlink.
It changes the entire row except for the hyperlink. I'm a little confused
about how the .inclass and .outclass works in the CSS part. I added it to
my page, but no change. I figure that I wasn't supposed to put .inclass
and
.outclass,but I don't know what to put in its place. Can you help just a
little more?
<style>
.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>

Thanks again!
--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System

Ken Cox said:
Hi Chuck,

I think your problem with CSS is that you're trying to change the class
of
the whole row and not just the hyperlink.

Here's some sample code that seems to do what you want. It changes the
Cssclass attribute of the hyperlink according to the inbound value.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>dghlnk</title>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<style>.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="In/Out">
<itemtemplate>
<asp:HyperLink ID="myhlink" runat="server"
Text='<%# DataBinder.Eval(Container, "DataItem.inout") %>' NavigateUrl='<%#
"http://www.aspalliance.com?id=" & DataBinder.Eval(Container,
"DataItem.Integervalue") %>' Target="_blank">
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim strInOut As String = DataBinder.Eval(e.Item.DataItem,
"inout")
hlnk = e.Item.FindControl("myhlink")
If strInOut = "In" Then
hlnk.CssClass = "inclass"
Else
hlnk.CssClass = "outclass"
End If
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("inout", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
If (i Mod 2) = 0 Then
dr(1) = "In"
Else
dr(1) = "Out"
End If
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


chuckdfoster said:
I have a hyperlink column in my datagrid. I want my hyperlink to be green
or red (depending on in/out status) no matter if the link has been visited
or not. How can I accomplish this? I have tried using CSS but that
doesn't
affect what is going on in my datagrid. This is what I have so far for
formatting in my datagrid...

Sub ItemBound(ByVal sender As Object, ByVal e as DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
'check inout field, if In then font is green, if Out then font
is
red
Dim strInOut as String = DataBinder.Eval(e.Item.DataItem, "inout")
If strInOut = "In" then
e.Item.ForeColor = System.Drawing.Color.Green
Else
e.Item.ForeColor = System.Drawing.Color.Red
End If
End If
End Sub

Any advice is greatly appreciated....Thanks in advance!
 
D

David Alpert

chuckdfoster said:
Thank you so much for the help, but I'm still having a little problem.

I understand what you mean about changing the whole row and not just the
hyperlink. I actually want to change the whole, including the hyperlink.
It changes the entire row except for the hyperlink. I'm a little confused
about how the .inclass and .outclass works in the CSS part. I added it to
my page, but no change. I figure that I wasn't supposed to put .inclass and
.outclass,but I don't know what to put in its place. Can you help just a
little more?
<style>
.inclass {
COLOR: green; TEXT-DECORATION: none
}
.outclass {
COLOR: red; TEXT-DECORATION: none
}
</style>

Sometimes CSS can be tricky if your HTML contains ID attributes mixed
in with CLASS attributes. ID attributes are sematically more specific
(supposed to be unique per page while CLASS attributes can occur many
times per page) and so generally take precedence.

Furthermore, CSS works with principles of inheritance and cascading,
so that if you have HTML with CLASS attributes nested inside HTML tag
with an ID attribute, those CLASS attributes can be selected and
styled separately from the same CLASS attribute outside that ID
container or inside another ID container.

Now, things get tricky in the HTML generated by ASP.NET because, so
far as i can tell, the HTML generated by ASP.NET is generally a mess.
For example, there always seem to be <span> tags with ID elements
scattered everywhere, especially in templated code:

<ul class=""><span id=""><li></li></span><span
id="><li></li></span></ul>

and this sometimes has interfered with my attempts to write clean and
simple style sheets for ASP.NET pages. Simple class rules
(.someclass) don't always seem to select "sometag" in the following
html:

<span id="long$_and$_unique$_aspnet$_clientID"><sometag
class="someclass" /></span>

Just something to consider when styling ASP.NET ....

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top