Results 1 to 14 of 14

Thread: Webform asp:image from resouces

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Smile Webform asp:image from resouces

    I’m creating an asp webform, where I have an image field. In my.resources I have 31 images: face_0 to face_30, which are all png files. How can I set the image on page_load? I want the image field to show face_0

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webform asp:image from resouces

    You wouldn't do it like that. You're still thinking in WinForms. Web Forms server controls generate HTML that is then sent to the browser. The HTML <img> tag has no support for image data. It only supports a URL for an image in the src attribute. Generally speaking, that URL should point to an image file in your site. The alternative would be for the URL to point to a script that returned image data and, inside that, you extracted the data from your resource and returned it in the appropriate format. You are not loading an image when you load your page. You're loading an HTML tag that then goes and gets the image.

    This is now a web site so there's no need to put files in resources to make downloading the app easier for individuals. There will be one deployment to one web server for everyone, so uploading a folder full of image files to your web host is not a problem.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webform asp:image from resouces

    Ok. I uploaded the images to my site. Got it at least displaying one image now...
    Why doesn't this code work?

    Code:
    Partial Public Class _Default
        Inherits System.Web.UI.Page
    
        Private WithEvents tmr As New Timer
        Private imageIndex As Integer = 1
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            clock.ImageUrl = "http://www.scproject.biz/clock_images/face_0.png"
            tmr.Interval = 250
            tmr.Enabled = True
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            MsgBox("here")
            clock.ImageUrl = "http://www.scproject.biz/clock_images/face_" & imageIndex.ToString & ".png"
            imageIndex += 1
            If imageIndex = 31 Then tmr.Enabled = False
        End Sub
    
    End Class

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webform asp:image from resouces

    The timer doesn't tick...

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Webform asp:image from resouces

    MsgBox isn't supported in ASP.NET. At least, it never used to be. I don't have the latest version of VS to test on to verify. If you need a popup message then you will need to do something like issue an alert via JavaScript.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Webform asp:image from resouces

    The timer stuff seems funkily implemented. I'm not sure this would work, but have you tried doing an AddHandler to tie the Timer you create to the tmr_Tick routine rather than the Handles?

    I'm not even sure that what you are trying to do would even work in ASP.NET, but I've never tried to implement a Timer on an ASP.NET web page that way and I don't have the ability to test right now either.

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webform asp:image from resouces

    MsgBox is working. I added a Timer in the Markup instead of in the code. Now it ticks but i can't start and stop it...

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Webform asp:image from resouces

    Quote Originally Posted by .paul. View Post
    MsgBox is working.
    Hmm, interesting. I'll have to fire up my VS box and see if I'm misremembering that it didn't exist with older versions of ASP.NET.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webform asp:image from resouces

    .paul., you need to stop and think a bit. You are now developing for the web STOP thinking like a Windows developer. As far as I'm aware, MsgBox does not work in a web app unless you're running the web server on the local machine. If your web server is in the UK and I'm viewing your site in a browser in Australia, do you think that MsgBox is going to display anything on my screen? What if someone was viewing your site on a Mac or a Chromebook or an iPhone? If you want to display something to the user of your app then you need to do so in the browser, which means using JavaScript in the page.

    As for your Timer, do you have your image in an UpdatePanel? If not then the best you can hope for is that your page will reload each time the Timer Ticks. If you want just the image to change without reloading the page then you need to use AJAX. Are you? I'd wager not.

    Seems to me that you're jumping into web development without considering the differences between Windows and web and throwing your hands up when such a difference presents itself. You should spend some time learning how web development works first and then develop to that, instead of butting into it constantly and wondering why.

  10. #10

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webform asp:image from resouces

    Thanks. Having an easier time building the GUI. The clock doesn't update at all. I switched to Javascript. I know my js timer is ticking. I'll investigate the UpdatePanel.

  11. #11

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webform asp:image from resouces

    The msgbox was for my benefit. Wouldn't have imagined changing an image would be so difficult. I could change the image that way in a standard Html, CSS, Javascript page...

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webform asp:image from resouces

    Quote Originally Posted by .paul. View Post
    I could change the image that way in a standard Html, CSS, Javascript page...
    Like I said, Microsoft tried to make web development work like Windows development with Web Forms but that's not natural for web development. As a result, you get friction in various scenarios because you think it should work the same way but it doesn't. If you just assume that it works differently and come at it from that angle, everything feels more natural. That's how I feel, anyway.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webform asp:image from resouces

    Quote Originally Posted by OptionBase1 View Post
    I'm not even sure that what you are trying to do would even work in ASP.NET, but I've never tried to implement a Timer on an ASP.NET web page that way and I don't have the ability to test right now either.
    I've never used a Timer in Web Forms either. I don't think it existed back when I worked with Web Forms, as it only dates back to .NET 3.5. Like the rest of Web Forms though, it's intended to look like WinForms to the developer but the implementation is completely different. Presumably it generates a JavaScript timer at the client end. It basically allows you to perform a post at specific intervals but if you're doing a full post then I suspect that the the image is being set back to its initial value each time the page is reloaded. You'd at least need to check IsPostBack in the Load event handler to prevent that happening. To just update an image as you would in WinForms, you'd need to use AJAX to perform a partial update of the page and, in Web Forms, that means using an UpdatePanel.

  14. #14
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Webform asp:image from resouces

    I switched to Javascript. I know my js timer is ticking. I'll investigate the UpdatePanel.
    If you need a Timer control in the web then you should really be look at doing it in JavaScript with SetInterval

    If you look up JavaScript timer you should get plenty of examples, you can also replace your image using JavaScript as well.

    As JavaScript is client side you can do all this without making any calls to the server
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width