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
ataGrid 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
ataGrid>
*******************************************************************************************
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
range;
}
.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