Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / March 2005

Tip: Looking for answers? Try searching our database.

Hyperlink Format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chuckdfoster - 08 Mar 2005 15:06 GMT
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!

Signature

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

Ken Cox [Microsoft MVP] - 09 Mar 2005 02:05 GMT
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

>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
[quoted text clipped - 18 lines]
>
> Any advice is greatly appreciated....Thanks in advance!
chuckdfoster - 09 Mar 2005 21:35 GMT
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!
Signature

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

> Hi Chuck,
>
[quoted text clipped - 123 lines]
> >
> > Any advice is greatly appreciated....Thanks in advance!
Ken Cox [Microsoft MVP] - 10 Mar 2005 02:40 GMT
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

> Thank you so much for the help, but I'm still having a little problem.
>
[quoted text clipped - 150 lines]
>> >
>> > Any advice is greatly appreciated....Thanks in advance!
David Alpert - 28 Mar 2005 22:50 GMT
> Thank you so much for the help, but I'm still having a little problem.
>
[quoted text clipped - 13 lines]
>         }
> </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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.