I have been struggling with setting an image source at runtime for a few days and figured it was time to post.
My C# app allows users to search for employees, and if found, displays information contained in a database, about the employee.
One of the pieces of information contained in the database is an image of the employee.
When the app loads, a placeholder image is loaded, so the image is not blank.
Once a searched for employee is found, the employee's image will be loaded from the database and replace the placeholder image.
Pretty straight-forward.
I can set the image "src" at design time, to contain the placeholder image.
I am not able to change the image source after the employee is searched for and found, however.
It took me a few hours to realize that I could not programmtically manipulate the image, due to the server control simply referencing a URL.
After searching for a solution, however, I found that using Page.FindControl, as follows, I could locate the image control and change the "ImageURL" properly.
This did not work, however, so I threw in a check for null, which returns null:
if
(myEmployeePicture == null)
{ lblSearchStatus.Text = "Null";
I thought perhaps a .dll was not loaded into the app, but reasoned against that as being the issue as the only error returned is at runtime, if I try to manipulate "myEmployeePicture" when it is null.
Thoughts on why I cannot locate the image control, so that I can change the "ImageUrl" properly?
Can you post the code. Is it an html control or asp control? If html then you can use asp variable place holder to do it from code behind. <img src="<%=Variable%>">
Don't forget to mark ANSWER if it helps you.
Marked as answer by techiejones on Jul 05, 2012 02:09 AM
Oddly, enough, as soon as I pasted the code, I noticed the "runat='server'" attribute was missing from the asp image control. Not sure how I overlooked that, but after I did that I re-ran the app. I received a runtime error to the effect of "Unable to
cast html image type to asp image type".
No worries, as I commented out that code that I pasted in my first post and tried to directly manipulate the asp image control, to which I could.
Running imgEmployeePicture.src ="images/test.jpg"; allowed me to programmatically manipulate the image source. I placed this code in the Page_Load event, just to test, and it worked.
So, it turns out the issue was a missing "runat='server'" attribute. Not sure how that happened, but I clearly overlooked.
TechieJones
Member
1 Points
4 Posts
Cannot set image source at runtime
Jul 04, 2012 10:47 PM|LINK
Hey all.
I have been struggling with setting an image source at runtime for a few days and figured it was time to post.
My C# app allows users to search for employees, and if found, displays information contained in a database, about the employee.
One of the pieces of information contained in the database is an image of the employee.
When the app loads, a placeholder image is loaded, so the image is not blank.
Once a searched for employee is found, the employee's image will be loaded from the database and replace the placeholder image.
Pretty straight-forward.
I can set the image "src" at design time, to contain the placeholder image.
I am not able to change the image source after the employee is searched for and found, however.
It took me a few hours to realize that I could not programmtically manipulate the image, due to the server control simply referencing a URL.
After searching for a solution, however, I found that using Page.FindControl, as follows, I could locate the image control and change the "ImageURL" properly.
System.Web.UI.WebControls.Image myEmployeePicture = (System.Web.UI.WebControls.Image)Page.FindControl("imgEmployeePicture");
This did not work, however, so I threw in a check for null, which returns null:
if (myEmployeePicture == null) { lblSearchStatus.Text = "Null";
I thought perhaps a .dll was not loaded into the app, but reasoned against that as being the issue as the only error returned is at runtime, if I try to manipulate "myEmployeePicture" when it is null.
Thoughts on why I cannot locate the image control, so that I can change the "ImageUrl" properly?
countycowpok...
Participant
1411 Points
340 Posts
Re: Cannot set image source at runtime
Jul 05, 2012 01:28 AM|LINK
TechieJones
Member
1 Points
4 Posts
Re: Cannot set image source at runtime
Jul 05, 2012 02:09 AM|LINK
It is an asp control:
<img alt="Employee Picture" id="imgEmployeePicture" src="images/placeholder.jpg" name="Employee Picture" />
Oddly, enough, as soon as I pasted the code, I noticed the "runat='server'" attribute was missing from the asp image control. Not sure how I overlooked that, but after I did that I re-ran the app. I received a runtime error to the effect of "Unable to cast html image type to asp image type".
No worries, as I commented out that code that I pasted in my first post and tried to directly manipulate the asp image control, to which I could.
Running imgEmployeePicture.src ="images/test.jpg"; allowed me to programmatically manipulate the image source. I placed this code in the Page_Load event, just to test, and it worked.
So, it turns out the issue was a missing "runat='server'" attribute. Not sure how that happened, but I clearly overlooked.
A second set of eyes helped.
Thanks for asking me to paste the code ;)
oned_gk
All-Star
31641 Points
6463 Posts
Re: Cannot set image source at runtime
Jul 05, 2012 02:20 AM|LINK
Change Page.FindControl("imgEmployeePicture") with Master.FindControl("imgEmployeePicture");
Is it work?
countycowpok...
Participant
1411 Points
340 Posts
Re: Cannot set image source at runtime
Jul 05, 2012 03:45 AM|LINK