Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 21st, 2009, 11:02 AM
Registered User
 
Join Date: Apr 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Random Function

I am using the following Random image generator for my background:

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim url As String = RotatePic("/Images/Home/")
        HomePage.Attributes.Add("style", "background:url(" + url + ")")
End Sub
 
Private Function RotatePic(ByVal directoryPath As String) As String
        Dim dirInfo As New DirectoryInfo(Server.MapPath(directoryPath))
        Dim fileList() As FileInfo = dirInfo.GetFiles()
        Dim numberOfFiles As Integer = fileList.Length
        Dim rnd As New Random
        Dim randomFileIndex As Integer = rnd.Next(numberOfFiles)
        Dim imageFileName As String = fileList(randomFileIndex).Name
        Dim fullImageFileName As String = Path.Combine(directoryPath, imageFileName)
        Return fullImageFileName
    End Function
This works most of the time, but sometimes I get no image background at all. How do I ensure that there always is an image?
  #2 (permalink)  
Old April 21st, 2009, 11:09 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. the error is no related with the random function. If the number generated by the fuction is higher than the filelist top, then you would receive an error. Do you see the file name that was generated when the error arrise???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old April 21st, 2009, 01:33 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Pretty easy to see you have two problems here. Good news is that neither of them involves the Random. That part is perfectly fine.

The main problem is that this code will never work if and when there is a space in the image name. This is because you have not placed single-quotes around the url in your Attributes.Add string. So, whenever there is a space in the URL, the browser's CSS interpreter chokes.

Here is the simple fix for that:

Code:
HomePage.Attributes.Add("style", "background:url('" + url + "')")
The second problem, which you may not actually care about, is that this function does not properly resolve the url coming back, so that this code will never work for pages below the root directory. Like I said, if this is for your home page, maybe that's okay with you. However, you can make this more flexible by fixing it so that it resolves correctly no matter where the page is in your folder structure.

Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim url As String = RotatePic("~/Images/Home/")
        HomePage.Attributes.Add("style", "background:url('" + url + "')")
    End Sub
 
    Function RotatePic(ByVal directoryPath As String) As String
        Dim dirInfo As New DirectoryInfo(Server.MapPath(directoryPath))
        Dim fileList() As FileInfo = dirInfo.GetFiles()
        Dim numberOfFiles As Integer = fileList.Length
        Dim rnd As New Random
        Dim randomFileIndex As Integer = rnd.Next(numberOfFiles)
        Dim imageFileName As String = fileList(randomFileIndex).Name
        Dim fullImageFileName As String = Path.Combine(directoryPath, imageFileName)
        Dim fullImageUrl As String = ResolveUrl(fullImageFileName)
      Return fullImageUrl
    End Function
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #4 (permalink)  
Old April 22nd, 2009, 04:11 PM
Registered User
 
Join Date: Apr 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

that doesn't seem to solve the problem. this is the home page, so the path is fine and all the images are named the same way (homeimage1.jpg, homeimage2.jpg). If I load the page and refresh a couple of times, the images fail to show up at least once. It is not browser specific, it happens in IE, Firefox, and Safari
  #5 (permalink)  
Old April 22nd, 2009, 04:21 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Wow, that is weird. Except for the circumstances I stated, I cannot think of any other reason this could be happening. I have reproduced your example on my end and refreshed a billion times and this never happens.

It would be interesting to see if I could reproduce your issue. Click on my name above and shoot me an email. I will respond, then you'll have my email address. Then, zip up your solution and send it to me, and let me take a look at it.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #6 (permalink)  
Old April 27th, 2009, 05:44 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Okay... I got your ZIP and figured out your problem.

In the ZIP you sent me, it "appears" as though there are only 5 files in Images/HomePage:

quote2.gif
quote2.gif
quote3.gif
quote4.gif
quote5.gif

However, there are actually 6 files in this directory. (You can confirm this by stepping through the code and inspecting the value of fileList.Length, which is 6.)

If you step through and inspect the value of fileList, you will see the 6th file is the Thumbs.db file.

Thumbs.db is a hidden operating system file, so by default you cannot see it in Windows Explorer. If you disable the option that hides protected operating system files, you will be able to see it right away.

As I mentioned, Thumbs.db is the last file in this directory. So, whenever randomFileIndex = 5, it pulls Thumbs.db from the directory, which of course will not display in the browser.

I confirmed this on the client side by using FireBug to inspect your HomePage div when it came up empty.

Code:
<div id="ctl00_cpMainContent_HomePage" class="Quotes" style="background: transparent url(Images/HomePage/Thumbs.db) repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"> </div>
Solution -- get rid of that thumbs.db file. Problem solved.

P.S. I fixed your solution and sent it back to you.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}

Last edited by Lee Dumond; April 28th, 2009 at 01:00 AM..
The Following User Says Thank You to Lee Dumond For This Useful Post:
sal79 (April 27th, 2009)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
random sort Bernardo Pacheco XSLT 9 September 30th, 2012 06:28 AM
Random Number Function Query rsm42 ASP.NET 1.0 and 1.1 Basics 7 May 16th, 2007 05:04 AM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM
Automatic email and Random function wasaan Classic ASP Basics 1 June 25th, 2003 01:41 AM



All times are GMT -4. The time now is 02:21 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.