copy code,drag or close
By image I am referring to a graphic file such as jpeg, png or gif image file. This was one of the problems I had to solve for this site. I wanted a consistent simple look for each page. Its helpful to say each page is article and each article is record in the database. Some records will have an image, some won’t. So rather than having an empty image tag appearing on the page for those records that don’t have an image I needed the system to try and display an image if there is one to display.
Rather than dragging and dropping an image control from the Visual Studio.NET toolbox the answer is to only create the control that displays the image if there is an image to display i.e. conditionally. This can be achieved by programmatically declaring an image control in the pages code-behind file.
C# developers can use this shortcut, add
using visual = System.Web.UI.WebControls.Image;
This is known as using an alias, you can do it with any assembly your code references, its not needed here but it does make coding easier.
There are several ways to achieve most programatic tasks like this. One way is to say that every page should be able to change its behaviour at runtime, meaning it should have a property which can be set by the record it is reading from the database, more specifically which is set by the particular field in a the record which 'says' I have a picture and i want it displayed.
Typically your code needs to read the field values in each record to determine what it displays, if one of those value is a boolean with 1 meaning there is an image or 0 meaning there is not a image then a simple conditional test can be used. The code that does this conditional test could then call then addpic() method ( in the code view above).