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
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now