Finding Hyperlinkcolumn

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The problem
is that I have no way to find it. There is no ID for this so I can't do a
findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

Pass the PositionID to the helper function and get back a CSS class string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
Ken Cox said:
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to find
the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the color
doesn't change at all. In my grid the 3rd row has the PositionID as 54.

It actually does seem to be working as the viewsource shows the classes set
to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"
DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}"
/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}" />
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

..theOrange {
color:orange;
}
..blueclass {color:blue}
..redclass {color:red}
..thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0" border="1"
id="DataGrid1" style="margin: 5px 5px 5px 5px; padding: 5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass" href="AddNewPositions.aspx?PositionID=52">Web
Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass" href="AddNewPositions.aspx?PositionID=29">WAN
Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">DTIF</font></td><td align="Center" width="10%"><font
face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass" href="AddNewPositions.aspx?PositionID=54">This
is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class, but
the color is not changing.

Thanks,

Tom
Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


tshad said:
I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so I
can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Actually, as posted, it does seem to work. If I take out the link to my css
file and just put in the 2 classes inline - it does change the colors
correctly. I assume that something in my .css file is causing the problem.
I am looking into that now and will let you know what the problem was.

Other than that, it seems to work great.

Thanks,

Tom

tshad said:
Ken Cox said:
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to find
the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the
color doesn't change at all. In my grid the 3rd row has the PositionID as
54.

It actually does seem to be working as the viewsource shows the classes
set to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"

DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}" />
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}" />
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

.theOrange {
color:orange;
}
.blueclass {color:blue}
.redclass {color:red}
.thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0"
border="1" id="DataGrid1" style="margin: 5px 5px 5px 5px; padding: 5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=52">Web Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=29">WAN Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">DTIF</font></td><td align="Center" width="10%"><font
face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass"
href="AddNewPositions.aspx?PositionID=54">This is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class,
but the color is not changing.

Thanks,

Tom
Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


tshad said:
I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so I
can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
tshad said:
Actually, as posted, it does seem to work. If I take out the link to my
css file and just put in the 2 classes inline - it does change the colors
correctly. I assume that something in my .css file is causing the
problem. I am looking into that now and will let you know what the problem
was.

Other than that, it seems to work great.

I found the problem, but not sure why it is happening.

Here is the pertinant part of my css file.
*******************************************
<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link {
color:#4AAABD;
}
a:active {
color:#4AAABD;
}
..blueclass {color:blue}
..redclass {color:red}
</style>
*******************************************

I have my links set to display as Teal.

What is happening is that the links in my css file are NOT being overwritten
by the class in the <a> tag.

<a class="redclass" href="AddNewPositions.aspx?PositionID=54">This is my
test</a>

I would have thought that the color in the css file would work for all links
that didn't specifically specifiy a color.

I assume I am wrong here.

You can see the differences in these 2 links (The first has the Teal in the
style and the 2nd does not).

http://www.payrollworkshop.com/samples/testClass1.htm

http://www.payrollworkshop.com/samples/testClass2.htm

Tom
Thanks,

Tom

tshad said:
Ken Cox said:
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to find
the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the
color doesn't change at all. In my grid the 3rd row has the PositionID
as 54.

It actually does seem to be working as the viewsource shows the classes
set to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"

DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}" />
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}" />
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

.theOrange {
color:orange;
}
.blueclass {color:blue}
.redclass {color:red}
.thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0"
border="1" id="DataGrid1" style="margin: 5px 5px 5px 5px; padding: 5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=52">Web Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=29">WAN Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">DTIF</font></td><td align="Center" width="10%"><font
face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass"
href="AddNewPositions.aspx?PositionID=54">This is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class,
but the color is not changing.

Thanks,

Tom
Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so I
can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Hi Tom,

Another thing you could try is narrowing down the a:link which is overriding
the class:

<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link.blueclass {
color:blue;
}
a:link.redclass {
color:red;
}
a:active {
color:#4AAABD;
}

</style>

Ken

tshad said:
tshad said:
Actually, as posted, it does seem to work. If I take out the link to my
css file and just put in the 2 classes inline - it does change the colors
correctly. I assume that something in my .css file is causing the
problem. I am looking into that now and will let you know what the
problem was.

Other than that, it seems to work great.

I found the problem, but not sure why it is happening.

Here is the pertinant part of my css file.
*******************************************
<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link {
color:#4AAABD;
}
a:active {
color:#4AAABD;
}
.blueclass {color:blue}
.redclass {color:red}
</style>
*******************************************

I have my links set to display as Teal.

What is happening is that the links in my css file are NOT being
overwritten by the class in the <a> tag.

<a class="redclass" href="AddNewPositions.aspx?PositionID=54">This is my
test</a>

I would have thought that the color in the css file would work for all
links that didn't specifically specifiy a color.

I assume I am wrong here.

You can see the differences in these 2 links (The first has the Teal in
the style and the 2nd does not).

http://www.payrollworkshop.com/samples/testClass1.htm

http://www.payrollworkshop.com/samples/testClass2.htm

Tom
Thanks,

Tom

tshad said:
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to
find the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the
color doesn't change at all. In my grid the 3rd row has the PositionID
as 54.

It actually does seem to be working as the viewsource shows the classes
set to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"

<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana,
Arial, Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"

DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}" />
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}"
/>
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

.theOrange {
color:orange;
}
.blueclass {color:blue}
.redclass {color:red}
.thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0"
border="1" id="DataGrid1" style="margin: 5px 5px 5px 5px; padding:
5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=52">Web Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems
</font></td><td align="Center" width="10%"><font face="Verdana, Arial,
Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=29">WAN Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">DTIF</font></td><td align="Center"
width="10%"><font face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass"
href="AddNewPositions.aspx?PositionID=54">This is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems
</font></td><td align="Center" width="10%"><font face="Verdana, Arial,
Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class,
but the color is not changing.

Thanks,

Tom

Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"

DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so
I can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Try this one instead... it keeps the colour of your "non-class" links:

<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link.blueclass {
color:blue;
}
a:link.redclass {
color:red;
}
a:link {
color:#4AAABD;
}

a:active {
color:#4AAABD;
}

</style>

tshad said:
tshad said:
Actually, as posted, it does seem to work. If I take out the link to my
css file and just put in the 2 classes inline - it does change the colors
correctly. I assume that something in my .css file is causing the
problem. I am looking into that now and will let you know what the
problem was.

Other than that, it seems to work great.

I found the problem, but not sure why it is happening.

Here is the pertinant part of my css file.
*******************************************
<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link {
color:#4AAABD;
}
a:active {
color:#4AAABD;
}
.blueclass {color:blue}
.redclass {color:red}
</style>
*******************************************

I have my links set to display as Teal.

What is happening is that the links in my css file are NOT being
overwritten by the class in the <a> tag.

<a class="redclass" href="AddNewPositions.aspx?PositionID=54">This is my
test</a>

I would have thought that the color in the css file would work for all
links that didn't specifically specifiy a color.

I assume I am wrong here.

You can see the differences in these 2 links (The first has the Teal in
the style and the 2nd does not).

http://www.payrollworkshop.com/samples/testClass1.htm

http://www.payrollworkshop.com/samples/testClass2.htm

Tom
Thanks,

Tom

tshad said:
Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to
find the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the
color doesn't change at all. In my grid the 3rd row has the PositionID
as 54.

It actually does seem to be working as the viewsource shows the classes
set to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"

<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana,
Arial, Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"

DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}" />
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}"
/>
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

.theOrange {
color:orange;
}
.blueclass {color:blue}
.redclass {color:red}
.thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0"
border="1" id="DataGrid1" style="margin: 5px 5px 5px 5px; padding:
5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=52">Web Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems
</font></td><td align="Center" width="10%"><font face="Verdana, Arial,
Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=29">WAN Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">DTIF</font></td><td align="Center"
width="10%"><font face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass"
href="AddNewPositions.aspx?PositionID=54">This is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems
</font></td><td align="Center" width="10%"><font face="Verdana, Arial,
Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class,
but the color is not changing.

Thanks,

Tom

Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"

DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so
I can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Ken Cox said:
Try this one instead... it keeps the colour of your "non-class" links:

<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link.blueclass {
color:blue;
}
a:link.redclass {
color:red;
}
a:link {
color:#4AAABD;
}

a:active {
color:#4AAABD;
}

</style>

This works great. Just what I was looking for (except Mozilla seems to have
a problem with it).

I assume this is some bug in Mozilla (but it isn't consistant).

If you look at http://www.payrollworkshop.com/Samples/testClass4.htm, you
should see that it work fine in IE and Firefox.

In Mozilla (and Netscape 7.0), it seems to not like the 3rd row and even
though it uses the same class, it shows as Teal and not as blue as it
should.

In my other test, I actually had about 10 rows that did this.

Not sure what is causing it - I do have to figure it out as I assume some
users will be using these browsers.

I assume that the original styles ( .blueclass {color:blue} and .redclass
{color:red}) didn't work as they were not for links.

Thanks,

Tom
tshad said:
tshad said:
Actually, as posted, it does seem to work. If I take out the link to my
css file and just put in the 2 classes inline - it does change the
colors correctly. I assume that something in my .css file is causing
the problem. I am looking into that now and will let you know what the
problem was.

Other than that, it seems to work great.

I found the problem, but not sure why it is happening.

Here is the pertinant part of my css file.
*******************************************
<style type="text/css">
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
a:visited {
color:#4AAABD;
}
a:link {
color:#4AAABD;
}
a:active {
color:#4AAABD;
}
.blueclass {color:blue}
.redclass {color:red}
</style>
*******************************************

I have my links set to display as Teal.

What is happening is that the links in my css file are NOT being
overwritten by the class in the <a> tag.

<a class="redclass" href="AddNewPositions.aspx?PositionID=54">This is my
test</a>

I would have thought that the color in the css file would work for all
links that didn't specifically specifiy a color.

I assume I am wrong here.

You can see the differences in these 2 links (The first has the Teal in
the style and the 2nd does not).

http://www.payrollworkshop.com/samples/testClass1.htm

http://www.payrollworkshop.com/samples/testClass2.htm

Tom
Thanks,

Tom

message Hi Tom,

I'd take a different approach, using a template column and a helper
function.

I like the idea, as it means I don't have to go through the items to
find the correct one. This is much cleaner.

But I can't seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the
color doesn't change at all. In my grid the 3rd row has the PositionID
as 54.

It actually does seem to be working as the viewsource shows the classes
set to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

*******************************************************************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;"

<HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana,
Arial, Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"

DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"

DataNavigateUrlFormatString="addScreenQuestions.aspx?PositionID={0}" />
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?PositionID={0}"
/>
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
*******************************************************************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

.theOrange {
color:orange;
}
.blueclass {color:blue}
.redclass {color:red}
.thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

*****************************************************************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0"
border="1" id="DataGrid1" style="margin: 5px 5px 5px 5px; padding:
5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana,
Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl0','')"><font
color="#2FABAD">Job Title</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl1','')"><font
color="#2FABAD">Date Posted</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack('DataGrid1$_ctl1$_ctl2','')"><font
color="#2FABAD">Company</font></a></b></font></td><td><font
face="Verdana, Arial, Helvetica, sans-serif" color="#2FABAD"><b>Screen
Questions</b></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif" color="#2FABAD"><b>Test Questions</b></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=52">Web Developer Senior</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=52">Web Developer
Senior</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">14Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=52">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=52">Test</a></font></td>

</tr><tr bgcolor="#E5E5E5">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="blueclass"
href="AddNewPositions.aspx?PositionID=29">WAN Hardware Technician</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=29">WAN Hardware
Technician</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">09Mar05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">DTIF</font></td><td align="Center"
width="10%"><font face="Verdana, Arial, Helvetica, sans-serif"><a
href="addScreenQuestions.aspx?PositionID=29">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=29">Test</a></font></td>

</tr><tr bgcolor="#F2F2F2">
<td><font face="Verdana, Arial, Helvetica, sans-serif">
<a class="redclass"
href="AddNewPositions.aspx?PositionID=54">This is my test</a>
</font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif"><a href="AddNewPositions.aspx?PositionID=54">This is my
test</a></font></td><td><font face="Verdana, Arial, Helvetica,
sans-serif">20Apr05</font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif">Cro-Magnon Systems </font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addScreenQuestions.aspx?PositionID=54">Screen</a></font></td><td
align="Center" width="10%"><font face="Verdana, Arial, Helvetica,
sans-serif"><a
href="addSkillstest.aspx?PositionID=54">Test</a></font></td>
******************************************************************************************

As you can see the the 1st <a> tags in each row have the correct class,
but the color is not changing.

Thanks,

Tom

Pass the PositionID to the helper function and get back a CSS class
string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass='<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>'
Text='<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>'
NavigateUrl='<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>'>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>


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

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", 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 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"

DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so
I can't do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom
 
Hi Tom,

Glad to hear it helps. You might want to do a search to see if there's a way
to get it to work in Mozilla. I don't know anything about that browser.

See you later!

Ken
 
Ken Cox said:
Hi Tom,

Glad to hear it helps. You might want to do a search to see if there's a
way to get it to work in Mozilla. I don't know anything about that
browser.

Actually, it wasn't a browser issue. It was that I needed to also change
the visited and active links. What was happening was that once I visited
the link, it went to visited (which wasn't set up for blue and red) so it
took the default - Teal.

BTW, is there a way to have it only take red if the positionID is equal some
number and the rest stay the default color? Using the following:

CssClass='<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>'

Would I do the return as just:

Return

instead of

Return "blueclass"

or would that cause an error.

Thanks,

Tom
 
Back
Top