I came across this problem today when trying to set the default image of an ASP.net image control. There is an option to set the alternate text but its not going to work, all this option does is to set a big red X. By doing some search on Bing I found a solution, the solution offered will work if you are using the code behind feature, if not, adding a script tag in the aspx page will create an error. here is the solution that was provided and then the solution I came up with when not using code behind,
Using code behind:
- First add the javascript in your <head> tag
1: <script type="text/javascript>
2:
3: function Defaultimage(img)
4:
5: {
6: img.src(Defaultimage.jpg)
7: }
8:
9: </script>
- add onerror attribute to your image control:
Note: The onerror attribute is not a standard attribute of the image control, but once rendered into HTML it will work just fine.
1: <asp:Image ID="lblPhoto" runat="server" BorderStyle="Solid" BorderWidth="3px" onerror="DisplayDefaultImage(this)"
2: BorderColor="#dcdcdc" ImageUrl="myimage.jpg"/>
When not using code behind
- add a scriptManager control to your page then set the path to your Javascript file.
1: <asp:ScriptManager ID="ScriptManager1" runat="server">
2: <Scripts>
3: <asp:ScriptReference Path="~/directory/DisplayDefaultImage.js" />
4: </Scripts>
5: </asp:ScriptManager>
- add onerror attribute to your image control:
1: <asp:Image ID="lblPhoto" runat="server" BorderStyle="Solid" BorderWidth="3px" onerror="DisplayDefaultImage(this)"
2: BorderColor="#dcdcdc" ImageUrl="myimage.jpg"/>