Changing hyperlink template based on data contents

M

MattB

I'm pretty new to all of this, so please forgive me if this is a
question you've heard too many times. I'm using VS2003 and vb.net in the
codebehind.

I'm trying to make a column in my datagrid have use one of two different
templates to build a hyperlink based on what's in the data. I think I'm
really close, but I can't quite get it to work.

I've got my column working as a hyperlink as a template column, and now I
need to add logic to change that hyperlink if a certain field is not empty.

So if the field is populated, it just uses this in the aspx page:
<asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#
DataBinder.Eval(Container, "DataItem.node_id", "ItemList.aspx?node_id={0}")
%>' Text='<%# DataBinder.Eval(Container, "DataItem.descrip") %>'>

I've created an OnItemDataBound event that seems to be firing as I can set
the text to italic if my "item" column is empty. Now I just need to change
the hyperlink within the same if/then. Can I do this here? Is this an OK
approach?

I want my new hyperlink to be something like I have above, but using a
concatenation of three fields for the data item:
DataBinder.Eval(Container, "DataItem.ColX" & "DataItem.ColY" &
"DataItem.ColZ", "ItemShow.aspx?XYZ={0}")

So in my OnDataBoundEvent I have this section where I test for an empty cell
and for now just make the text italic:

If Not Trim(e.Item.DataItem("item")) = "" Then
'it's an item
e.Item.Cells.Item(0).Font.Italic = True
End If

Next I want to change the hyperlink here too if I can. Is that possible? I
was thinking I could do something like:

e.Item.Cells.Item(0).Controls.Hyperlink = "ItemShow.aspx?item=" &
e.Item.DataItem("ColX") & e.Item.DataItem("ColY") e.Item.DataItem("ColZ")

But that first object reference (e.Item.Cells.Item(0).Controls.Hyperlink) is
not correct. Can anyone help me here? TIA!
 
M

MattB

MattB said:
I'm pretty new to all of this, so please forgive me if this is a
question you've heard too many times. I'm using VS2003 and vb.net in
the codebehind.

I'm trying to make a column in my datagrid have use one of two
different templates to build a hyperlink based on what's in the data.
I think I'm really close, but I can't quite get it to work.

I've got my column working as a hyperlink as a template column, and
now I need to add logic to change that hyperlink if a certain field
is not empty.

So if the field is populated, it just uses this in the aspx page:
<asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#
DataBinder.Eval(Container, "DataItem.node_id",
"ItemList.aspx?node_id={0}") %>' Text='<%# DataBinder.Eval(Container,
"DataItem.descrip") %>'>

I've created an OnItemDataBound event that seems to be firing as I
can set the text to italic if my "item" column is empty. Now I just
need to change the hyperlink within the same if/then. Can I do this
here? Is this an OK approach?

I want my new hyperlink to be something like I have above, but using a
concatenation of three fields for the data item:
DataBinder.Eval(Container, "DataItem.ColX" & "DataItem.ColY" &
"DataItem.ColZ", "ItemShow.aspx?XYZ={0}")

So in my OnDataBoundEvent I have this section where I test for an
empty cell and for now just make the text italic:

If Not Trim(e.Item.DataItem("item")) = "" Then
'it's an item
e.Item.Cells.Item(0).Font.Italic = True
End If

Next I want to change the hyperlink here too if I can. Is that
possible? I was thinking I could do something like:

e.Item.Cells.Item(0).Controls.Hyperlink = "ItemShow.aspx?item=" &
e.Item.DataItem("ColX") & e.Item.DataItem("ColY")
e.Item.DataItem("ColZ")

But that first object reference
(e.Item.Cells.Item(0).Controls.Hyperlink) is not correct. Can anyone
help me here? TIA!

I'm happy to say I've solved this on my own. Here's the codebehind in case
anyone else gets stumped by this one:

Sub ItemGrid_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
Dim linkCell As TableCell
linkCell = e.Item.Controls(0)
Dim h As HyperLink

If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
h = linkCell.Controls(0)

If Not Trim(e.Item.DataItem("item")) = "" Then
'it's an item
e.Item.Cells.Item(0).Font.Italic = True
h.NavigateUrl = "itemShow.aspx?DCI=" &
e.Item.DataItem("department") & e.Item.DataItem("category") &
e.Item.DataItem("item")
Else
h.NavigateUrl = "itemList.aspx?node_id=" &
e.Item.DataItem("node_id")
End If
End If
End Sub

Matt
 
Q

QUASAR

Hi Matt,

I've tryed your code but it seems doesn't work for me...
It not seems to me changing I've made are wrong but somewhere the error must
be...

Can someone give a check to my sub?

Thanks,
Giorgio

'---------------------------------------------------------------------------
-----------
Sub ItemGrid_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)

If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then

If e.Item.DataItem("_ID_genre") < 2 Then
' genre is "ancillary"
e.Item.BackColor = System.Drawing.Color.Yellow
e.Item.Cells.Item(0).BackColor =
System.Drawing.Color.FromName("Green")
End If

End If

End Sub

'---------------------------------------------------------------------------
 
Q

QUASAR

Ok now it works...
was my fault, I've not setted in the datagrid properties the
"OnItemDataBound" event...

If someone may have the same need the final code was here...
Insert the sub and don't forgot to set in the datagrid properties the
"OnItemDataBound" event
<asp:DataGrid id="DataGrid1"...
....OnItemDataBound="DataGrid1_ItemDataBoundEvent"

'---------------------------------------------------------------------------
----------

Sub DataGrid1_ItemDataBoundEvent(sender as Object, e as
DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item _
OR e.Item.ItemType = ListItemType.AlternatingItem then

'Check to see if the genre ID is 1 (Ancillary)
If e.Item.DataItem("_ID_Tipo_Item") < 2 Then
e.Item.BackColor = System.Drawing.Color.FromName("Lavender")
End If

End If

End Sub

'---------------------------------------------------------------------------
 
Q

QUASAR

Sorry to had posted here, not in line with the subject
but was because I start from the code given to suite my needs...
(change item row color based on the current item field value...)

Anyway now it works...
was my fault, I've not setted in the datagrid properties the
"OnItemDataBound" event...
Eheheh sorry!

If someone may have the same need the final code was here...
Insert the sub and don't forgot to set in the datagrid properties the
"OnItemDataBound" event
<asp:DataGrid id="DataGrid1"...
....OnItemDataBound="DataGrid1_ItemDataBoundEvent"

'---------------------------------------------------------------------------
----------

Sub DataGrid1_ItemDataBoundEvent(sender as Object, e as
DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item _
OR e.Item.ItemType = ListItemType.AlternatingItem then

'Check to see if the genre ID is 1 (Ancillary)
If e.Item.DataItem("_ID_Tipo_Item") < 2 Then
e.Item.BackColor = System.Drawing.Color.FromName("Lavender")
End If

End If

End Sub

'---------------------------------------------------------------------------
 

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
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top