Alternating Item Style "ForeColor" not working in Template Column

G

Guest

Hi,
I have a datagrid built that has an alternating item style that sets the
backcolor and ForeColor of its rows. I have 4 template columns. One of them
has a LinkButton embedded in it to perform updates. All the styles that are
set are being followed by all templated columns with the exception for the
update column. The update column does set the BackColor appropriately but
when setting its ForeColor the LinkButton's color is blue and remains blue
for all rows. However in another column which is a ButtonColumn with a
linkbutton in it as well for editing the colors are alternating fine. I
cannot make the update column a ButtonColumn because it needs to be able to
CauseValidation on the form and I could not find a property within the
ButtonColumn to do that. I tried removing all styles from the style sheet
pertaining to links but then my general windows environment settings started
to take over. If anyone has any clues as to how I can alternate the
forecolor I would greatly appreciated it.
Thanks in advance,
Max
 
G

Guest

Just to clarify before I answer, you want to make a LinkButton column in a
DataGrid to alternate BackColor?

If so, you may have to put a function in for the BackColor value.... a
simple if then else routine to alternate the color value, placing it in there.
 
G

Guest

No I am trying to alternate the forecolor.... the LinkButtons TextColor. The
BackColor is alternating which leads me to believe that its not changing the
forecolor because it is a link and something is overriding the
alternatingitemstyle for the datagrid. Right now the linkbutton's text is
blue in every row. I need it to be blue/white/blue etc.
 
G

Guest

Ahh....
The Styles/Css are determining it's coloring. You may want to start with
adding a cssClass to that item, but in an alternating way. Perhaps use a <%=
%> to hold the cssClass where the value is a function that retrieves an
alternating value

myColor = blue
Sub GetColor() as String
if(myColor=blue) then
myColor=white
else
myColor=blue
end if
return myColor
End Sub


.....untested but it should be enough to get you started...

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
 
G

Guest

That might work if I was defining my controls in the aspx. file but I have a
base template page class. I am defining all my controls in the code behind.
 
B

Bruce Barker

link buttons are implemented with an anchor (<a>). anchors have psuedo css
styles for their "visited" state. you have to define the forecolor for each
of these states (visited,link,active, and hover).


-- bruce (sqlwork.com)
 
G

Guest

Thanks for the advice curt but this still does not help me. If i try to set
the value for my linkbutton in Item_DataBound or Item_Created I get a object
not set to an instance of an object. Here is my code... The linkbutton
resides in a template column. What is wierd is the ButtonColumn that I
created that has an edit linkbutton alternates color on the grid. Why are
the TemplateColumn and ButtonColumn objects acting differently.

Public Class UpdateITemplateCol
Implements ITemplate

Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)
Implements System.Web.UI.ITemplate.InstantiateIn
Dim btn As New LinkButton
btn.Text = "Update"
btn.ID = "Update"
btn.CommandName = "Update"

btn.CausesValidation = True
container.Controls.Add(btn)
End Sub

End Class


Public Class BaseTemplate
Inherits System.Web.UI.Page


'''Item Databound DG Event''''
Private Sub dgBedInfo_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgBedInfo.ItemDataBound

Dim LinkBtn As LinkButton = CType(e.Item.FindControl("Update"),
LinkButton)
LinkBtn.ForeColor = GetUpdateLinkColor()

End Sub

'''Item Created DG Event"""
Private Sub dgBedInfo_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgBedInfo.ItemCreated

Dim LinkBtn As LinkButton = CType(e.Item.FindControl("Update"),
LinkButton)
LinkBtn.ForeColor = GetUpdateLinkColor()

End Sub

'''Alternating Color Sub'''
Private Function GetUpdateLinkColor() As System.Drawing.Color
'AlternatingColor is a session variable accessed through public property
If (AlternatingColor = "Blue") Then
AlternatingColor = "White"
Return System.Drawing.Color.Blue
Else
AlternatingColor = "Blue"
Return System.Drawing.Color.White
End If
End Function

End Class
 
G

Guest

Bruce thanks,
Yes, I know that already. That may have something to do with the reason why
my links are staying blue in the templated column. However I have set an
alternatingItemstyle for the datagrid that the column is in.

dgBedInfo.AlternatingItemStyle.BackColor = Color.SlateGray
dgBedInfo.AlternatingItemStyle.ForeColor = Color.White

Now... I have a buttoncolumn as well... that also has a linkbutton in it
too. This linkbutton follows the style correctly. It alternates the
linkcolor. Just not in the templatecolumn. Why is the buttoncolumn
performing as expected and not the templatecolumn.
 
G

Guest

Curt-

You were right about changing the color in the Item_DataBound event. My
problem at first is how I was trying to access the control. I was trying the
following way Dim LinkBtn As LinkButton = CType(e.Item.FindControl("Update"),
LinkButton). And each time I would get a Object reference not set exception.
Instead of trying to reference it by the Id "Update" that I had given it. I
iterated through the e.item.controls collection and grabbed each
control(cell) and then I went through the control collection of the cell.

Private Sub dgBedInfo_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgBedInfo.ItemDataBound
Dim ctrl As Control
For Each ctrl In e.Item.Controls
Dim i As Integer
For i = 0 To ctrl.Controls.Count - 1
Dim s As String = ctrl.Controls(i).GetType().ToString()
If (s = "System.Web.UI.WebControls.LinkButton") Then
CType(ctrl.Controls(i), LinkButton).ForeColor =
GetUpdateLinkColor()
End If
Next
Next
End Sub

Thanks,
Max
 

Ask a Question

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

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top