Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional logic in DataList?

Status
Not open for further replies.

song2siren

Programmer
Jun 4, 2003
103
GB
Hello

I just need to find out the best way to add some conditional logic to a DataList. I've got the following button in my ItemTemplate:

<asp:HyperLink NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Runat="server"><asp:ImageButton ID="AddtoCart" ImageUrl="Images/addtocart.gif" runat=server /></asp:HyperLink>

However, if an InStock field in my SQL database displays the bit value '0', I need to drop the HyperLink and ImageButton and just display the text, 'Out of Stock'. This seems so simple, but I'm getting nowhere!

Any help would be very much appreciated.
 
Hi - many thanks for the link.

I've managed to create a function which can display an 'out of stock' message, but I can't find a way to disable the HyperLink control. Changing the image is fine, but users will still be able to add the product to their cart.

Any suggestions would be much appreciated.

Thanks again
 
Hi,

You need to check for what you want in the ItemDataBound event:

Sub MyDataList_ItemDataBound(ByVal sources As Object, ByVal e As DataListItemEventArgs) Handles MyDataList.ItemDataBound

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

If <whatever you need to check>
Then
e.Item.FindControl("yourhyperlink").Visible = True
Else
e.Item.FindControl("yourhyperlink").Visible = False
End If
End If

End Sub

You can make a hyperlink enabled or not as well.

hth
j
 
Hi - thanks for the comment. I've solved the problem by using this function to display an 'out of stock' message:

Protected Function CheckInStock(InStock As Object) As String

If InStock = False Then
'pubsLink.Enabled = false
Return "<span class='Text'><font color='#FF0000'>Out Of Stock</font></span>"
Else
Return ""
End If

End Function

And I've used the following to disable the link and button:

<asp:HyperLink ID="pubsLink" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# CType(DataBinder.Eval(Container.DataItem, "InStock"), Boolean) %>' Runat="server" ><asp:ImageButton ID="AddtoCart" ImageUrl="Images/addtocart.gif" Visible='<%# Container.DataItem("InStock") %>' runat=server /></asp:HyperLink>

Seems to work fine, but I can see your method is just as effective.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top