Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Condtional Fromatting Within a Datalist


Message #1 by "Hugh McLaughlin" <hugh@k...> on Thu, 25 Apr 2002 06:31:48
Hello Everyone and thanks for your help in advance.  I am working on an 
application that uses a dataset to read data from a SQL Server table and 
populate a datalist.  An example can be located at:

http://www.1naples.com/tools/AdWizard.aspx

The problem that I am encountering involves the graphics.  As you can see, 
the graphic that should be in place is referecned by the field MLS# which 
in turn is dynamically inserted into the <img> tag to reference the 
graphic.  But in situations like the example, I want to be able to test if 
the photo actually exists, and if not specify a path to a default 
graphic.  I know how to use the FileSystem objects to determine whether 
the picture exists or not, but I am unclear as to how to change the 
graphic name since it is located within a datalist and I am defining the 
value as <img  src="images/<%# Container.DataItem("MLNUMBER") %>"> and 
then using Page.Databind() to set the value.  I am not sure if this can be 
done with a datalist or how.  any help would be greatly appreciated.  
Thanks.
Message #2 by Imar Spaanjaars <Imar@S...> on Thu, 25 Apr 2002 08:03:59 +0200
Hi Hugh,

You can pass in the value of the MLNumber to a custom function, that 
returns the img tag only when the image is present.

<ASPX page>
<td class="blt"><%# LinkAttachment(Cstr(Container.DataItem("Image") & "")) 
%></td>


Then in your code behind, use this:

Public Function LinkAttachment(ByVal sImage As String) As String
If Len(iImage) = 0 Then
         LinkAttachment = "&nbsp;"
Else
         LinkAttachment = "<img src=""/images/" & sAttachmentID & """>"
End If
End Function

There may be better / faster / easier ways to do this, but this worked for me.

HtH

Imar



At 06:31 AM 4/25/2002 +0000, you wrote:
>Hello Everyone and thanks for your help in advance.  I am working on an
>application that uses a dataset to read data from a SQL Server table and
>populate a datalist.  An example can be located at:
>
>http://www.1naples.com/tools/AdWizard.aspx
>
>The problem that I am encountering involves the graphics.  As you can see,
>the graphic that should be in place is referecned by the field MLS# which
>in turn is dynamically inserted into the <img> tag to reference the
>graphic.  But in situations like the example, I want to be able to test if
>the photo actually exists, and if not specify a path to a default
>graphic.  I know how to use the FileSystem objects to determine whether
>the picture exists or not, but I am unclear as to how to change the
>graphic name since it is located within a datalist and I am defining the
>value as <img  src="images/<%# Container.DataItem("MLNUMBER") %>"> and
>then using Page.Databind() to set the value.  I am not sure if this can be
>done with a datalist or how.  any help would be greatly appreciated.
>Thanks.


Message #3 by "Harish" <harish@h...> on Thu, 25 Apr 2002 09:25:14 -0500
And also datalist has "OnItemDataBound" event which gets fired for every
item. So you can write a function which you can call on this event.

<asp:DataList id="DataList1" OnItemDataBound="CustomFormat"
runat="server"></asp:DataList>

And in the code behind function you can customize any field:
Function CustomFormat(ByVal Source As Object, ByVal E As
DataGridItemEventArgs)
			If isNumeric(E.Item.Cells(1).Text) Then
				E.Item.BackColor = System.Drawing.Color.LemonChiffon
			Else
				E.Item.BackColor = System.Drawing.Color.FromArgb(0, 235, 240, 250)
			End If
End Function

HTH.

Harish.


-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Thursday, April 25, 2002 1:04 AM
To: ASP+
Subject: [aspx] Re: Condtional Fromatting Within a Datalist


Hi Hugh,

You can pass in the value of the MLNumber to a custom function, that
returns the img tag only when the image is present.

<ASPX page>
<td class="blt"><%# LinkAttachment(Cstr(Container.DataItem("Image") & ""))
%></td>


Then in your code behind, use this:

Public Function LinkAttachment(ByVal sImage As String) As String
If Len(iImage) = 0 Then
         LinkAttachment = "&nbsp;"
Else
         LinkAttachment = "<img src=""/images/" & sAttachmentID & """>"
End If
End Function

There may be better / faster / easier ways to do this, but this worked for
me.

HtH

Imar



At 06:31 AM 4/25/2002 +0000, you wrote:
>Hello Everyone and thanks for your help in advance.  I am working on an
>application that uses a dataset to read data from a SQL Server table and
>populate a datalist.  An example can be located at:
>
>http://www.1naples.com/tools/AdWizard.aspx
>
>The problem that I am encountering involves the graphics.  As you can see,
>the graphic that should be in place is referecned by the field MLS# which
>in turn is dynamically inserted into the <img> tag to reference the
>graphic.  But in situations like the example, I want to be able to test if
>the photo actually exists, and if not specify a path to a default
>graphic.  I know how to use the FileSystem objects to determine whether
>the picture exists or not, but I am unclear as to how to change the
>graphic name since it is located within a datalist and I am defining the
>value as <img  src="images/<%# Container.DataItem("MLNUMBER") %>"> and
>then using Page.Databind() to set the value.  I am not sure if this can be
>done with a datalist or how.  any help would be greatly appreciated.
>Thanks.




Message #4 by "Hugh McLaughlin" <hugh@k...> on Fri, 26 Apr 2002 03:58:26
Hi Imar,

As always, thanks for your response.  I guess I am having problems making 
the leap from classic ASP to ASP.Net, so your response (and Harish's) 
make some sense, but I am not completely clear.  Basically what I am 
trying to accomplish is to check to see if a corresponding image reside 
within the "images" folder.  If it does, the <img> tag is generated with 
the proper ID.  However, if the image deson't exsit, a default picture 
with a corresponding image tag is inserted.  Where I am confused with 
your explanation (and Harish's) is looping through each record in the 
DataList, checking to see if the picture exists.  Does yours and Harish's 
example do this.  Also, I have no experience with OnItemDatabound, so I 
am unclear how to use this.  Could you please walk me through this a 
little more?

Again, thanks for your help.

Hugh
> Hi Hugh,

You can pass in the value of the MLNumber to a custom function, that 
returns the img tag only when the image is present.

<ASPX page>
<td class="blt"><%# LinkAttachment(Cstr(Container.DataItem("Image") 
& "")) 
%></td>


Then in your code behind, use this:

Public Function LinkAttachment(ByVal sImage As String) As String
If Len(iImage) = 0 Then
         LinkAttachment = "&nbsp;"
Else
         LinkAttachment = "<img src=""/images/" & sAttachmentID & """>"
End If
End Function

There may be better / faster / easier ways to do this, but this worked 
for me.

HtH

Imar



At 06:31 AM 4/25/2002 +0000, you wrote:
>Hello Everyone and thanks for your help in advance.  I am working on an
>application that uses a dataset to read data from a SQL Server table and
>populate a datalist.  An example can be located at:
>
>http://www.1naples.com/tools/AdWizard.aspx
>
>The problem that I am encountering involves the graphics.  As you can 
see,
>the graphic that should be in place is referecned by the field MLS# which
>in turn is dynamically inserted into the <img> tag to reference the
>graphic.  But in situations like the example, I want to be able to test 
if
>the photo actually exists, and if not specify a path to a default
>graphic.  I know how to use the FileSystem objects to determine whether
>the picture exists or not, but I am unclear as to how to change the
>graphic name since it is located within a datalist and I am defining the
>value as <img  src="images/<%# Container.DataItem("MLNUMBER") %>"> and
>then using Page.Databind() to set the value.  I am not sure if this can 
be
>done with a datalist or how.  any help would be greatly appreciated.
>Thanks.


Message #5 by Imar Spaanjaars <Imar@S...> on Fri, 26 Apr 2002 08:04:40 +0200
Hi Hugh,

The function I showed you will fire for each row in the datalist. 
Basically, with most data bound controls, you define behavior for one row 
that will be fired for each row in the result set. So, in the function I 
showed you (or indeed, better yet, the ItemCreated event) for each row in 
the result set, the function is called with the parameter specified.
Inside the function you could check to see if the file exists and if it 
does, write out the <img> tag.

Check out the ItemCreated event at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclassonitemdataboundt
opic.asp

And check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiodirectoryclasstopic.asp

for more info about the File object of the System.IO namespace.

HtH

Imar




At 03:58 AM 4/26/2002 +0000, you wrote:
>Hi Imar,
>
>As always, thanks for your response.  I guess I am having problems making
>the leap from classic ASP to ASP.Net, so your response (and Harish's)
>make some sense, but I am not completely clear.  Basically what I am
>trying to accomplish is to check to see if a corresponding image reside
>within the "images" folder.  If it does, the <img> tag is generated with
>the proper ID.  However, if the image deson't exsit, a default picture
>with a corresponding image tag is inserted.  Where I am confused with
>your explanation (and Harish's) is looping through each record in the
>DataList, checking to see if the picture exists.  Does yours and Harish's
>example do this.  Also, I have no experience with OnItemDatabound, so I
>am unclear how to use this.  Could you please walk me through this a
>little more?
>
>Again, thanks for your help.
>
>Hugh
> > Hi Hugh,
>
>You can pass in the value of the MLNumber to a custom function, that
>returns the img tag only when the image is present.
>
><ASPX page>
><td class="blt"><%# LinkAttachment(Cstr(Container.DataItem("Image")
>& ""))
>%></td>
>
>
>Then in your code behind, use this:
>
>Public Function LinkAttachment(ByVal sImage As String) As String
>If Len(iImage) = 0 Then
>          LinkAttachment = "&nbsp;"
>Else
>          LinkAttachment = "<img src=""/images/" & sAttachmentID & """>"
>End If
>End Function
>
>There may be better / faster / easier ways to do this, but this worked
>for me.
>
>HtH
>
>Imar
>


Message #6 by "Hugh McLaughlin" <hugh@k...> on Fri, 26 Apr 2002 12:19:12
Hi Imar,

As always thanks for the help.  I reviewed the articles and your code and 
got it working perfectly.  More importantly, I now understand the 
concept.  Again, thanks for your consideration.

Best Regards,
Hugh


> Hi Hugh,

The function I showed you will fire for each row in the datalist. 
Basically, with most data bound controls, you define behavior for one row 
that will be fired for each row in the result set. So, in the function I 
showed you (or indeed, better yet, the ItemCreated event) for each row in 
the result set, the function is called with the parameter specified.
Inside the function you could check to see if the file exists and if it 
does, write out the <img> tag.

Check out the ItemCreated event at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclassonitemdataboundtopic
.asp

And check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemiodirectoryclasstopic.asp

for more info about the File object of the System.IO namespace.

HtH

Imar




At 03:58 AM 4/26/2002 +0000, you wrote:
>Hi Imar,
>
>As always, thanks for your response.  I guess I am having problems making
>the leap from classic ASP to ASP.Net, so your response (and Harish's)
>make some sense, but I am not completely clear.  Basically what I am
>trying to accomplish is to check to see if a corresponding image reside
>within the "images" folder.  If it does, the <img> tag is generated with
>the proper ID.  However, if the image deson't exsit, a default picture
>with a corresponding image tag is inserted.  Where I am confused with
>your explanation (and Harish's) is looping through each record in the
>DataList, checking to see if the picture exists.  Does yours and Harish's
>example do this.  Also, I have no experience with OnItemDatabound, so I
>am unclear how to use this.  Could you please walk me through this a
>little more?
>
>Again, thanks for your help.
>
>Hugh
> > Hi Hugh,
>
>You can pass in the value of the MLNumber to a custom function, that
>returns the img tag only when the image is present.
>
><ASPX page>
><td class="blt"><%# LinkAttachment(Cstr(Container.DataItem("Image")
>& ""))
>%></td>
>
>
>Then in your code behind, use this:
>
>Public Function LinkAttachment(ByVal sImage As String) As String
>If Len(iImage) = 0 Then
>          LinkAttachment = "&nbsp;"
>Else
>          LinkAttachment = "<img src=""/images/" & sAttachmentID & """>"
>End If
>End Function
>
>There may be better / faster / easier ways to do this, but this worked
>for me.
>
>HtH
>
>Imar
>



  Return to Index