Evaluate items in datalist in

Posted on 2013-01-29
Last Modified: 2013-01-30
I have a datalist and the last item of the list is a link, but I want to make sure the file exists it is supposed to be linking to before doing the link.  How do I evaluate the items seperately?

This is the code I attempted, it does not link to anything.

                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblStatus" runat="server" text='<%# Eval("Status") %>'/>
                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblStatementNumber" runat="server" text='<%# Eval("StatementNumber") %>' />
                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblStatementDate" runat="server" text='<%# Eval("StatementDate") %>' />
                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblDueDate" runat="server" text='<%# Eval("DueDate") %>'/>
                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblBilledAMT" runat="server" text='<%# Eval("BilledAMT") %>'/>
                    <td valign="top" width="14%" align="left">
                        <asp:Label ID="lblOutstandingAMT" runat="server" text='<%# Eval("OutstandingAMT") %>' />
                    <td valign="top" width="14%" align="left">
                        <asp:HyperLink ID="HyperLink1" runat="server" Text="PDF" ImageUrl="~/Images/pdficon_small.gif" Target="_blank"
                        navigateurl = '<% if (My.Computer.FileSystem.FileExists("\\\Statements\"Eval("StatementNumber")) _
                                then "" & Eval("StatementNumber") & ".pdf"
                        end if
                        <asp:HyperLink ID="lnkFormat" runat="server" Text="PDF" ImageUrl="~/Images/pdficon_small.gif" Target="_blank" />

Open in new window

Question by:UnderSeven
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 83

Assisted Solution

CodeCruiser earned 200 total points
ID: 38831761
You can use the DataList.ItemDataBound event to add this logic.

Your line

'<% if (My.Computer.FileSystem.FileExists("\\\Statements\"Eval("StatementNumber"))

is missing a &

'<% if (My.Computer.FileSystem.FileExists("\\\Statements\"Eval("StatementNumber"))

Author Comment

ID: 38833048
This event you mentioned, is there an event I could use for a link on a datalist clicked?

So for instance, removing the part of the code that does logic on the aspx side, could I use an event for that link clicked that then used the datalist.itemdatabound to determine which one it was one when clicked "statementnumber" and then do all of the work in the code behind?  

Could you point me in the right direction?  I prefer to do this in the code behind.
LVL 83

Expert Comment

ID: 38833089
Yes you can. In the ItemDataBound event, subscribe to the LinkClicked event of the link and you can handle it in code behind.
LVL 41

Accepted Solution

guru_sami earned 300 total points
ID: 38833116
Here's how you can get your link and statement number in the ItemDataBound event.

Protected Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles DataList1.ItemDataBound
	If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
		Dim drow As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
		Dim hl As HyperLink = DirectCast(e.Item.FindControl("HyperLink1"), HyperLink)
			'You have link and statement number.
			'Manipulate them as you like
		Dim sn As String = drow("StatementNumber").ToString()
	End If
End Sub

Open in new window


Author Closing Comment

ID: 38835612
These solutions were really quite cool.  I didn't quite grasp the power of the itemdatabound event until guru_sami's response, or I might not of asked about on click.  You both answered my questions, and I'm thrilled to understand this functionality better.

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question