| nkPic"), HyperLink)
'Set the Text and Navigation properties hl.Text = Path.GetFileNameWithoutExtension(_ DataBinder.Eval(e.Item.DataItem, "Name").ToString()) & _ " (" & _ Int(DataBinder.Eval(e.Item.DataItem, "Length") / 1000) & _ " KB)" hl.NavigateUrl = "Default.aspx?N=" & e.Item.ItemIndex End If End Sub
HTML部分代码
<asp:DataList runat="server" id="dlIndex" OnItemDataBound="dlIndex_ItemDataBound" RepeatColumns="3"> <ItemTemplate> <li><asp:HyperLink runat="server" id="lnkPic" /></li> </ItemTemplate> </asp:DataList>
在上面的代码中,在DATALIST的onitemdatabound事件中,首先判断当前触发的项目是否是列表项listitemtype或者是交替项AlternatingItem,如果是的话,则动态生成链接hl,设置hl的值为当前正在浏览图象的文件名,并且注明了文件的大小,设置其链接的地址为当前浏览图象的地址,这样,用户可以直接点要浏览的图片了,不一要通过上一张,下一张的链接来实现。
最后给出运行的一个例子(http://aspnet.4guysfromrolla.com/London/)和全部代码:
<%@ Import Namespace="System.IO" %> <script runat="server" language="VB"> Sub Page_Load(sender as Object, e as EventArgs) Dim dirInfo as New DirectoryInfo(Server.MapPath("")) Dim images() as FileInfo = FilterForImages(dirInfo.GetFiles())
Dim imgIndex as Integer = 0
If Not Request.QueryString("N") is Nothing AndAlso IsNumeric(Request.QueryString("N")) then imgIndex = CInt(Request.QueryString("N")) End If
currentImgTitle.Text = "You are Viewing: " & _ Path.GetFileNameWithoutExtension(images(imgIndex).Name) & _ " (" & imgIndex + 1 & " of " & images.Length & ")" currentImg.ImageUrl = Path.GetFileName(images(imgIndex).Name)
If imgIndex > 0 then lnkPrev.NavigateUrl = "Default.aspx?N=" & imgIndex - 1 End If 上一页 [1] [2] [3] [4] [5] 下一页 |