-
Jul 15th, 2020, 06:40 PM
#1
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 07:55 PM
#2
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.
-
Jul 15th, 2020, 07:56 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 08:22 PM
#4
Re: Webform asp:image from resouces
The timer doesn't tick...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 08:22 PM
#5
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.
-
Jul 15th, 2020, 08:30 PM
#6
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.
-
Jul 15th, 2020, 08:37 PM
#7
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 08:49 PM
#8
Re: Webform asp:image from resouces
Originally Posted by .paul.
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.
-
Jul 15th, 2020, 09:01 PM
#9
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.
-
Jul 15th, 2020, 10:14 PM
#10
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 10:16 PM
#11
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2020, 10:51 PM
#12
Re: Webform asp:image from resouces
Originally Posted by .paul.
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.
-
Jul 15th, 2020, 10:55 PM
#13
Re: Webform asp:image from resouces
Originally Posted by OptionBase1
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.
-
Jul 17th, 2020, 03:41 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|