I'm trying to write a simple .net picture gallery and well.... it's not turning out to be that simple for me.
I have an aspx page with the following table/control in it
<table align=center>
<tr>
<td>Gallery</td>
</tr>
<asp:Repeater ID="picRepeater" Runat="server">
<ItemTemplate>
<tr>
<td><img src="http://pics.10026.com/?src=<%# picsArray(0)%>" alt="<%# picsArray(0)%>"></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
And then in my code behind i have a page load event that looks like the following...
Dim GalleryName As String = Request.QueryString("id")
Dim path As String = Server.MapPath("galleries/" & GalleryName)
Dim galleryDirectory As New DirectoryInfo(path)
Dim picsArray As Array = galleryDirectory.GetFiles("*.jpg")
picRepeater.DataSource = picsArray
picRepeater.DataBind()
Now I keep getting an error telling me that picsArray is not defined.
I don't understand.
For all i know i'm doing the whole thing wrong all together - anyone?Not sure if this is the problem, but try changing your picsArray declaration to this:
Dim picsArray() As String = galleryDirectory.GetFiles("*.jpg")
Ignore my previous post.
The problem is in here:
<img src="http://pics.10026.com/?src=<%# picsArray(0)%>" alt="<%# picsArray(0)%>">
Try changing it to:
<img src='<%# Container.dataitem(0)%>' alt='<%# Container.dataitem(0)%>'>
Not sure of the exact syntax for getting an array element out of the dataitem, but it will be something like the above.
The problem is that you can't refer to the datasource variable by name in the itemtemplate.
It will always be some form of container.dataitem.
Thanks jwsample... that much has worked.
Now i'm getting a new error
"No default member found for type 'FileInfo'"
Ok this is happening everytime i use a repeater control
I usually populate an array, bind it to my repeater control and then call Container.DataItem(0) to output it...
Where am i going wrong?
Dim path As String = Server.MapPath("galleries")
Dim galleries As New DirectoryInfo(path)
Dim myArray As Array
myArray = galleries.GetDirectories()
galleryList.DataSource = myArray
galleryList.DataBind()
With a repeater control...
<asp:Repeater id="galleryList" runat="server">
<ItemTemplate>
<p><%# Container.DataItem(0).Tostring()%></p>
</ItemTemplate>
</asp:Repeater>
I keep getting "No default member found for type 'DirectoryInfo'" as an error
getDirectories returns an array of DirectoryInfo objects, not strings.
So, you could probably do:
Databinder.Eval(Container.Dataitem,"Name")
Completely untested, so I don't know if this will work or not.
Also, just as a general rule, you should try to strongly type your array variables. so yo can catch more errors at compile time.
For example, change these 2 lines:
Dim myArray As Array
myArray = galleries.GetDirectories()
to:
Dim myArray() As DirectoryInfo
myArray = galleries.GetDirectories()
0 comments:
Post a Comment