I've seen this posted a few times, but no satisfactory answer...any ideas would be greatly appreciated...
What I am trying to achieve is create a photo album type page with a main picture, then a bunch of thumbnails. When a thumbnail is selected, it will update the main picture.
I want the thumbnails to be generated automatically from a directory which is selected in a dropdown, hence the for next loop. The main picture will update if the image button is selected (via the
I get a "Server tags cannot contain <% ... %> constructs" error. I would like to know if there's a way around about this error as it has caused me other problems, but failing that what alternatives do I have.
Any replies greatfully received. The effect is rather cool, it will automatically create a photo album with none of the preparation normally required,
ie just point to your pics directory and everything else is created....big time saver. Will pass on the relevant aspx file if I get this resolved if anyone needs it.
Happy coding
Cheers
Mikey
'Code below
'********
<%
Dim files() As System.IO.FileInfo 'Array of .jpg in a chosen directory
Dim strName As
String 'This will be the filename xxxx.jpj
Dim url As
String 'url to update main picture
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
'Populate files() with 'jpgs
For Each f
As System.IO.FileInfo
In files '****Create a new imagebutton (thumbnail) for each .jpg in directory
If Right$(f.Name, 4) =
".jpg" Then
strName = f.Name
url = "C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text &
"\" & ddnFiles.Text '**Get url from directory from 2 x dropdowns
%>
<asp:ImageButton
id= <%=strName %>
runat="server" '***Create the thumbnail THIS IS THE OFFENDING LINE CAUSING ERROR
AlternateText
= "Sort Ascending" '***Not important
ImageUrl = <% =url %> '*Display image filename from dropdown
OnCommand =
"ImageButton_Command" '*****Tells control to jump to sub below on selection
CommandName =
"Sort" '***Not important
CommandArgument=<% =strName%> 'Parameter to pass to main picture via sub below
Height="41px"
Width="55px"/>
<%
End If
Next
%>
'**********************
'**********************
Sub ImageButton_Command(ByVal sender
As Object,
ByVal e As CommandEventArgs) 'Enter this sub when imagebutton selected
hypPic.ImageUrl = "~/Guest/images/Pics/" & ddnList.Text &
"/" & e.CommandArgument '***This is the main picture
ddnFiles.Text = e.CommandArgument 'Updates the dropdown to reflect picture selected
End Sub
'**********************
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Public Sub BindData()
' Sets the dataset to the folder of files.
Me.DataList1.DataSource = GetDataset()
Me.DataList1.DataBind()
End Sub
Public Function GetDataset() As System.Data.DataSet
Dim Path As String = "/AVX2/eBook/PSP/img/"Dim F() As String = System.IO.Directory.GetFiles(Server.MapPath(Path), "*.jpg")
Dim DS As New System.Data.DataSet("Results")
Dim DT As Data.DataTable = DS.Tables.Add("Images")
' The string array to add rows
Dim S(2) As String' Add our columns to the datasource.
DT.Columns.Add("Image")
DT.Columns.Add("Description")
DT.Columns.Add("ID")
For A As Integer = 0 To F.Length - 1
S(0) = Path + System.IO.Path.GetFileName(F(A))
S(1) = System.IO.Path.GetFileNameWithoutExtension(F(A))
S(2) = "Picture" + A.ToString
DT.Rows.Add(S)
Next
Return DS
End Function
Specializing in ASP.NET 2.0
Marked as answer by MikeyBoyd on Oct 10, 2006 11:16 AM
OMG...you guys are stars. I will try those very elegant pieces of work...
I did almost get a solution using purely code behind (preferred by pro's but I'm a humble hobbyist newbie)..however the 'OnCommand' doesn't appear to be available programmatically? OnClientClick is however.
Thanks once again.
Mikey
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
For Each f
As System.IO.FileInfo
In files
If Right$(f.Name, 4) =
".jpg" Then
Dim myImage
As ImageButton = New ImageButton()
Dim j As
Integer
j = j + 1
'Set the label's Text and ID properties.
myImage.ID = CStr(j)
myImage.ImageUrl = "~\Guest\images\Pics\" & ddnList.Text &
"\" & f.Name
myImage.Height = 40
myImage.Width = 50
myImage.ToolTip = f.Name
myImage.OnCommand = "ImageButton_Command" 'Not available programatically??
myImage.CommandName = "NA"
myImage.CommandArgument = f.Name
myImage.AlternateText = f.Name
PlaceHolder1.Controls.Add(myImage)
Dim spacer As LiteralControl =
New LiteralControl(" ")
'Add a spacer in the form of a single space
PlaceHolder1.Controls.Add(spacer)
End If
Got it, thanks for the help. For those of you interested, this is the full code for a basic but effective Phot Gallery, key features I was looking for were:-
- Automatically pull all files within a directory and create a main image with thumbnails for all images, select thumnail to update main image
- Dropdown to select directory and image
Only preparation I would suggest are
1. Resize images (MS Powertoys works a treat)
2. Keep the number of images in each directory to a minimum since it loads all images in all their glory, so may be sloow
Next challenge is to add
1. Slideshow (update to next image every 5 secs or so)
2. Text boxes for resizing to other defaults (current is good for 1024x768 and images are 640x480)
2. See if I can find a way to only load the main pic on each post back (either frames or may require Atlas/Ajax) - any tips here greatfully received.
--------------------------
The Problem
--------------------------
Error is:
Server tags cannot contain <% ... %> constructs
Problem deals with the order in which .NET does things.
In ASP, we used to be able to do this ...
<a href="/help/<%=MyHelpPage%>.asp">hyperlink</a>
and ...
<%=MyHelpPage%> ... got substituted with the string value.
So I tried something similar with ASP hyperlink control ...
<asp:HyperLink NavigateUrl="~/help/<%=MyHelpPage%>.aspx" ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>
Above is WRONGLY translated to HTML like this ...
href="~/help/<%=MyHelpPage%>/.aspx"
and every thing else I tried ended with: Server tags cannot contain <% ... %> constructs
Bottom line ... it don't work!
So ... how to accomplish similar funcationality in ASP.NET
---------------------------
The Solution (in VB)
---------------------------
Partial Class mywebpage
Public mHelpPageURL As String = "~/help/use1_help.aspx#"
Protected Sub Page_Load(...)
Me.Page.DataBind()
End Sub
End Class
Because of the order in which ASP.NET processes things, the solution needs DATA BINDING to work. The # sign in the asp <%...%> tells the compiler that this is a DataBound value. The exact syntax between the <% and %> depends on the language being used (VB
or C#). In the code behind, notice Page.DataBind(). That's the call which does the work.
Why would you use databinding for a single ASP Control not contained in an ITemplate? You would be better assigning the Asp Hyperlink controls NavigateUrl property in the code as databinding incurs more overhead.
Rgds, Martin.
For the benefit of all users please mark any post answers as appropriate.
Why? Because I'm supporting a 25,000 line ASP application which we're converting (piece-meal) to ASP.NET 2.0 and most pages in the original website have 10-20 of these type embedded substitutions. (smile) Otherwise, I wouldn't (grin) You know the drill,
"Just Make It Work!".
Rick, Thank you for the above code! It took me hours to finally find a good example, because no matter what I do, my javascript will not recognize the "Image1" control.
I really don't think I had to change anything from your code example except the Path in the code behind.
The thumbnails display when the page loads, but the main image does not. When you click a thumbnail, I get to the first alert box with it displaying a valid URL to display the image. However the next line does not work. I get the following error message
when I click on the yellow exclamation in the left corner of the browser.
Line: 165
Char: 5
Error: 'document.getElementById(...)' is null or not an object
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
If Not Page.IsPostBack
Then
BindData()
End If
End Sub
Public
Sub BindData()
' Sets the dataset to the folder of files.
Me.DataList1.DataSource = GetDataset()
Me.DataList1.DataBind()
End Sub
Public
Function GetDataset()
As System.Data.DataSet
Dim Path As
String = "SecUploadedImages/"
Dim F()
As String = System.IO.Directory.GetFiles(Server.MapPath(Path),
"*.jpg")
Dim DS As
New System.Data.DataSet("Results")
Dim DT As Data.DataTable = DS.Tables.Add("Images")
' The string array to add rows
Dim S(2) As
String
' Add our columns to the datasource.
DT.Columns.Add("Image")
DT.Columns.Add(
"Description")
DT.Columns.Add("ID")For A
As Integer = 0
To F.Length - 1
MikeyBoyd
Member
33 Points
13 Posts
Server tags cannot contain <% ... %> constructs
Oct 08, 2006 09:26 PM|LINK
I've seen this posted a few times, but no satisfactory answer...any ideas would be greatly appreciated...
What I am trying to achieve is create a photo album type page with a main picture, then a bunch of thumbnails. When a thumbnail is selected, it will update the main picture.
I want the thumbnails to be generated automatically from a directory which is selected in a dropdown, hence the for next loop. The main picture will update if the image button is selected (via the
I get a "Server tags cannot contain <% ... %> constructs" error. I would like to know if there's a way around about this error as it has caused me other problems, but failing that what alternatives do I have.
Any replies greatfully received. The effect is rather cool, it will automatically create a photo album with none of the preparation normally required,
ie just point to your pics directory and everything else is created....big time saver. Will pass on the relevant aspx file if I get this resolved if anyone needs it.
Happy coding
Cheers
Mikey
'Code below
'********
<%
Dim files() As System.IO.FileInfo 'Array of .jpg in a chosen directory
Dim strName As String 'This will be the filename xxxx.jpj
Dim url As String 'url to update main picture
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles() 'Populate files() with 'jpgs
For Each f As System.IO.FileInfo In files '****Create a new imagebutton (thumbnail) for each .jpg in directory
If Right$(f.Name, 4) = ".jpg" Then
strName = f.Name
url = "C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text & "\" & ddnFiles.Text '**Get url from directory from 2 x dropdowns
%>
<asp:ImageButton id= <%=strName %> runat="server" '***Create the thumbnail THIS IS THE OFFENDING LINE CAUSING ERROR
AlternateText = "Sort Ascending" '***Not important
ImageUrl = <% =url %> '*Display image filename from dropdown
OnCommand = "ImageButton_Command" '*****Tells control to jump to sub below on selection
CommandName = "Sort" '***Not important
CommandArgument=<% =strName%> 'Parameter to pass to main picture via sub below
Height="41px" Width="55px"/>
<%
End If
Next
%>
'**********************
'**********************
Sub ImageButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs) 'Enter this sub when imagebutton selected
hypPic.ImageUrl = "~/Guest/images/Pics/" & ddnList.Text & "/" & e.CommandArgument '***This is the main picture
ddnFiles.Text = e.CommandArgument 'Updates the dropdown to reflect picture selected
End Sub
'**********************
Rick Rataycz...
Member
652 Points
140 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 09, 2006 12:41 AM|LINK
Wrap that in a DataList. Here's a web user control that can do what you want.
<img id="Image1" border="0" alt="Preview" /> <br /><br /> <div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" Width="100%"> <ItemTemplate> <div style="border: 1px outset;"> <div> <img id="<%# Eval("ID") %>" onclick="ShowImage(this.src);" src="<%# Eval("Image") %>" border="0" align="absmiddle" style="cursor: hand; width: 50px;" /> </div> <div> <%# Eval("Description") %> </div> </div> </ItemTemplate> </asp:DataList> </div> <script type="text/javascript"> <!-- function ShowImage(i) { document.getElementById('Image1').src = i; } //--> </script>And the code behind:
mokeefe
Star
10850 Points
2098 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 09, 2006 01:33 AM|LINK
ASP.Net 2.0
Adding the images from code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ImagesList.aspx.vb" Inherits="ImagesList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Partial Class ImagesList
Inherits System.Web.UI.Page
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
' ASP.Net 2.0
' NOTE Page has placeholder with ID PlaceHolder1
' Create the dynamic controls
' using app root for sample
Dim dirInf As New IO.DirectoryInfo(Server.MapPath("/"))
' filter on vb files - you would use jpg or a combination of alternates such as *.bmp
Dim fls() As IO.FileInfo = dirInf.GetFiles("*.vb")
' this will output the images in flow layout, if you want more of a straight list
' you would add each image to a parent container that had width assinged to an
' appropriate size to wrap the images
For Each fl As IO.FileInfo In fls
' Build an aspx image and add to placeholder
' NOTE Using an image button to handle click event!
Dim img As New ImageButton
' I'm setting alternate text as I won't assignt eh imageurl
img.AlternateText = fl.FullName
' the other image properties
'.......
AddHandler img.Click, AddressOf Me.ImageButton_Click
' add to placeholder
Me.PlaceHolder1.Controls.Add(img)
Next
' As for server tags in a server control - thats a negative.
End Sub
Protected Sub ImageButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Dim imgBtn As ImageButton = sender
imgBtn.AlternateText = "Been Cliked " & imgBtn.AlternateText
End Sub
End Class
Rgds,
Martin.
Martin.
For the benefit of all users please mark any post answers as appropriate.
MikeyBoyd
Member
33 Points
13 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 09, 2006 12:08 PM|LINK
OMG...you guys are stars. I will try those very elegant pieces of work...
I did almost get a solution using purely code behind (preferred by pro's but I'm a humble hobbyist newbie)..however the 'OnCommand' doesn't appear to be available programmatically? OnClientClick is however.
Thanks once again.
Mikey
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
For Each f As System.IO.FileInfo In files
If Right$(f.Name, 4) = ".jpg" Then
Dim myImage As ImageButton = New ImageButton()
Dim j As Integer
j = j + 1
'Set the label's Text and ID properties.
myImage.ID = CStr(j)
myImage.ImageUrl = "~\Guest\images\Pics\" & ddnList.Text & "\" & f.Name
myImage.Height = 40
myImage.Width = 50
myImage.ToolTip = f.Name
myImage.OnCommand = "ImageButton_Command" 'Not available programatically??
myImage.CommandName = "NA"
myImage.CommandArgument = f.Name
myImage.AlternateText = f.Name
PlaceHolder1.Controls.Add(myImage)
Dim spacer As LiteralControl = New LiteralControl(" ") 'Add a spacer in the form of a single space
PlaceHolder1.Controls.Add(spacer)
End If
MikeyBoyd
Member
33 Points
13 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 09, 2006 02:52 PM|LINK
Got it, thanks for the help. For those of you interested, this is the full code for a basic but effective Phot Gallery, key features I was looking for were:-
- Automatically pull all files within a directory and create a main image with thumbnails for all images, select thumnail to update main image
- Dropdown to select directory and image
Only preparation I would suggest are
1. Resize images (MS Powertoys works a treat)
2. Keep the number of images in each directory to a minimum since it loads all images in all their glory, so may be sloow
Next challenge is to add
1. Slideshow (update to next image every 5 secs or so)
2. Text boxes for resizing to other defaults (current is good for 1024x768 and images are 640x480)
2. See if I can find a way to only load the main pic on each post back (either frames or may require Atlas/Ajax) - any tips here greatfully received.
Here it is
'******************
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Di As New System.IO.DirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics")
Dim files() As System.IO.FileInfo
Dim SubDir() As System.IO.DirectoryInfo = Di.GetDirectories()
If IsPostBack = False Then
For Each sdi As System.IO.DirectoryInfo In SubDir
Me.ddnList.Items.Add(sdi.Name)
Next
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
For Each f As System.IO.FileInfo In files
If Right$(f.Name, 4) = ".jpg" Then
Me.ddnFiles.Items.Add(f.Name)
End If
Next
End If
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
For Each f As System.IO.FileInfo In files
If Right$(f.Name, 4) = ".jpg" Then
Dim myImage As ImageButton = New ImageButton()
Dim j As Integer
j = j + 1
'Set the label's Text and ID properties.
myImage.ID = CStr(j)
myImage.ImageUrl = "~\Guest\images\Pics\" & ddnList.Text & "\" & f.Name
myImage.Height = 40
myImage.Width = 50
myImage.ToolTip = f.Name
'myImage.OnCommand = "ImageButton_Command"
myImage.CommandName = "NA"
myImage.CommandArgument = f.Name
myImage.AlternateText = f.Name
AddHandler myImage.Command, AddressOf Me.ImageButton_Command
PlaceHolder1.Controls.Add(myImage)
'Add a spacer in the form of an HTML <BR> element
Dim spacer As LiteralControl = New LiteralControl(" ")
PlaceHolder1.Controls.Add(spacer)
End If
Next
End Sub
Protected Sub ddnFiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
imgMain.ImageUrl = "~/Guest/images/Pics/" & ddnList.Text & "/" & ddnFiles.Text
' hypPic.NavigateUrl = "~/Guest/Slideshows/" & ddnList.Text & "/" & ddnFiles.Text
End Sub
Protected Sub ddnList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim files() As System.IO.FileInfo
ddnFiles.Items.Clear()
files = My.Computer.FileSystem.GetDirectoryInfo("C:\Inetpub\wwwroot\Guest\images\Pics\" & ddnList.Text).GetFiles()
For Each f As System.IO.FileInfo In files
If Right$(f.Name, 4) = ".jpg" Then
Me.ddnFiles.Items.Add(f.Name)
End If
Next
imgMain.ImageUrl = "~/Guest/images/Pics/" & ddnList.Text & "/" & ddnFiles.Text
End Sub
Sub ImageButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
imgMain.ImageUrl = "~/Guest/images/Pics/" & ddnList.Text & "/" & e.CommandArgument
'imgMain.Text = e.CommandArgument
ddnFiles.Text = e.CommandArgument
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript">
<!--
function TABLE1_onclick() {
}
// -->
</script>
<br />
<asp:DropDownList ID="ddnList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddnList_SelectedIndexChanged">
</asp:DropDownList> <asp:DropDownList ID="ddnFiles" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddnFiles_SelectedIndexChanged">
</asp:DropDownList><div style="text-align: center">
<br />
<table>
<tr>
<td style="width: 89px">
<asp:ImageButton ID="imgMain" runat="server" Height="400px" ImageUrl="~/Guest/images/Pics/Bell Island/Bell Island (1).jpg" Width="550px" /></td>
</tr>
</table>
<br />
</div>
<hr />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</asp:Content>
Image Viewer Server Tags
mokeefe
Star
10850 Points
2098 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 10, 2006 03:14 AM|LINK
Glad to be of help.
You might wish to tick both answer from Rick and myself.
You might also not e that both examples show how to filter the files based on file extension which would negate the later review on each file.
happy coding.
Martin.
- Some other stuff you might be interested in.
' dynamically add a table
Dim Table As New HtmlControls.HtmlTable
' build a row
Dim rw As New HtmlControls.HtmlTableRow
Dim cell As New HtmlControls.HtmlTableCell
rw.Controls.Add(cell)
'Add image button to cell
Dim imageBtn As New ImageButton()
imageBtn.AlternateText = "No Image Set"
cell.Controls.Add(imageBtn)
' add row to table
Table.Controls.Add(rw)
' add table to placeholder
PlaceHolder1.Controls.Add(Table)
'add placeholder to page
' You would add to your contentplaceholder not the form!
Me.form1.Controls.Add(PlaceHolder1)
' Using generic HTML Controls
'add a div
Dim dv As New HtmlControls.HtmlGenericControl("div")
dv.InnerText = "I am dynamically added"
' put in cell beside image
cell.Controls.Add(dv)
' end add *****************
Martin.
For the benefit of all users please mark any post answers as appropriate.
volking
Member
44 Points
22 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 17, 2007 02:26 PM|LINK
--------------------------
The Problem
--------------------------
Error is:
Server tags cannot contain <% ... %> constructs
Problem deals with the order in which .NET does things.
In ASP, we used to be able to do this ...
<a href="/help/<%=MyHelpPage%>.asp">hyperlink</a>
and ...
<%=MyHelpPage%> ... got substituted with the string value.
So I tried something similar with ASP hyperlink control ...
<asp:HyperLink NavigateUrl="~/help/<%=MyHelpPage%>.aspx" ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>
Above is WRONGLY translated to HTML like this ...
href="~/help/<%=MyHelpPage%>/.aspx"
and every thing else I tried ended with: Server tags cannot contain <% ... %> constructs
Bottom line ... it don't work!
So ... how to accomplish similar funcationality in ASP.NET
---------------------------
The Solution (in VB)
---------------------------
Code in markup ...
<asp:HyperLink NavigateUrl='<%#mHelpPageURL & "bookmark" %>' ID="HyperLink1" runat="server">
<img src="images/myicon.GIF" border="0" />
</asp:HyperLink>
Code in Code Behind
Partial Class mywebpage
Public mHelpPageURL As String = "~/help/use1_help.aspx#"
Protected Sub Page_Load(...)
Me.Page.DataBind()
End Sub
End Class
Because of the order in which ASP.NET processes things, the solution needs DATA BINDING to work. The # sign in the asp <%...%> tells the compiler that this is a DataBound value. The exact syntax between the <% and %> depends on the language being used (VB or C#). In the code behind, notice Page.DataBind(). That's the call which does the work.
mokeefe
Star
10850 Points
2098 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 18, 2007 01:26 PM|LINK
Hi Fred,
Why would you use databinding for a single ASP Control not contained in an ITemplate? You would be better assigning the Asp Hyperlink controls NavigateUrl property in the code as databinding incurs more overhead.
Martin.
For the benefit of all users please mark any post answers as appropriate.
volking
Member
44 Points
22 Posts
Re: Server tags cannot contain <% ... %> constructs
Oct 19, 2007 04:22 AM|LINK
Why? Because I'm supporting a 25,000 line ASP application which we're converting (piece-meal) to ASP.NET 2.0 and most pages in the original website have 10-20 of these type embedded substitutions. (smile) Otherwise, I wouldn't (grin) You know the drill, "Just Make It Work!".
wufpakbob
Member
2 Points
1 Post
Re: Server tags cannot contain <% ... %> constructs
Nov 28, 2007 07:50 PM|LINK
Rick, Thank you for the above code! It took me hours to finally find a good example, because no matter what I do, my javascript will not recognize the "Image1" control.
I really don't think I had to change anything from your code example except the Path in the code behind.
The thumbnails display when the page loads, but the main image does not. When you click a thumbnail, I get to the first alert box with it displaying a valid URL to display the image. However the next line does not work. I get the following error message when I click on the yellow exclamation in the left corner of the browser.
Line: 165
Char: 5
Error: 'document.getElementById(...)' is null or not an object
Code: 0
Url: http://localhost:3318/paid2pixelclick/Default3.aspx
Below is the code as I just ran it:
****************************************************************************
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default3.aspx.vb" Inherits="Default3" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<img src="" runat="server" id="Image1" alt="Preview" /><
br /><br /><
div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Width="100%"> <ItemTemplate> <div style="border: 1px outset;"> <div> <img alt="" id="<%# Eval("ID") %>" onclick="ShowImage(this.src);" src="<%# Eval("Image") %>" style="border:0; vertical-align:middle; cursor: hand; width: 50px;" /> </div> <div> <%# Eval("Description") %> </div> </div> </ItemTemplate> </asp:DataList></
div><
script type="text/javascript"><!--
function ShowImage(i) {alert(i);
//Error occurs on this next line: document.getElementById('Image1').src = i; document.getElementById('Image1').src = i;alert(
"Did I get here? No I did not.");}
//-->
</script></
asp:Content> ************************************************************************ Code Behind Below
**********************************************************
Partial Class Default3Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack ThenBindData()
End If End Sub Public Sub BindData() ' Sets the dataset to the folder of files. Me.DataList1.DataSource = GetDataset() Me.DataList1.DataBind() End Sub Public Function GetDataset() As System.Data.DataSet Dim Path As String = "SecUploadedImages/" Dim F() As String = System.IO.Directory.GetFiles(Server.MapPath(Path), "*.jpg") Dim DS As New System.Data.DataSet("Results") Dim DT As Data.DataTable = DS.Tables.Add("Images") ' The string array to add rows Dim S(2) As String ' Add our columns to the datasource. DT.Columns.Add("Image")DT.Columns.Add(
"Description") DT.Columns.Add("ID")For A As Integer = 0 To F.Length - 1S(0) = Path + System.IO.Path.GetFileName(F(A))
S(1) = System.IO.Path.GetFileNameWithoutExtension(F(A))
S(2) = "Picture" + A.ToStringDT.Rows.Add(S)
Next Return DSEnd FunctionEnd
Class******************************************************************
Thanks in advance for your help,
Bob