Evaluate items in datalist in asp.net

Posted on 2013-01-29
Medium Priority
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 "https://secure.a2gov.org/epayments/statements/" & 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 800 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 1200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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