Is it possible to display the image?
The CategoryName was able to display fine,
but the CategoryImage displayed as array of byte
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#Eval("CategoryImage")%> <%#Eval("CategoryName")%>
</ItemTemplate>
</asp:Repeater>
SqlDataReader dtrImage = cmdSelect.ExecuteReader();
Repeater1.DataSource = dtrImage;
Repeater1.DataBind();
Output:
Animal System.Byte[]
Food System.Byte[]
2
Answers
Not only is this possbible, but I fact recommend doing this.
You can even do this for files as opposed to say the image in a database.
(the reason why is security – you OFTEN don’t want files mapped to a folder that users can type in a URL to get say a PDF or whatever.
So, you read the file, and stream it out.
The nice part is any image, image button or whatever you place on the form can be sent the picture, and you don’t require some silly handler or "some other" place beyond the code.
So, say we have a simple repeater like this
(kind of card view layout).
You note the image control in above.
So, we have a image in the database.
About the only requirement is of course you need to have saved in that database not only the file (as raw byte data), but you need the file extension.
So, for above, we fill the repeater with this code:
So, that loads the repeater rather nice.
So, the only part left is to fill out the image.
In fact, this works even without a repeater anyway.
So, for each "repeat", we need to pull the image from the database.
So, in most cases, that means we use the row data bound event of the repeater.
thus, this:
And the result is now this:
And note how I have a button in the repeater. the code for that "view" button, as a example code is this:
That allows me to get the row index, the database PK, and even example code to pull one value out of that repeater.
output:
Edit: Above only really great for a few SMALLER images
So, often to just display a few thumb nails etc., then I do suggest just a stream out of the image from the database as a base64 string is fine.
HOWEVER, and this is a big however:
If you doing to display more then a few images, and you want that page to have post-backs (in place of say some link button to move on in the application), then as suggested in comments?
Then, it near always BETTER to use + build a handler for this.
This is especially if the repeater and web page in question will and does need post-backs. (if it does not, then turn off viewstate for the image control).
So, a 2nd approach (and often the most common example). is thus to add a handler.
So, the handler (add->new item->Generaic handler).
So, that handler would look like this:
And now the markup for the image control can look like this:
So, the result is much the same, but a BIG whopper of a difference is the 2nd example will cache the image, and if you need/want/expect to have post-backs on that page, then the 2nd example is STRONG suggested if the images are anything more then a rather small type of thumbnail preview.
With the first example, the images are not cached, and if you need post-backs, then the view state will actaully be sending the images BACK to the server when you post-back. This thus can be rather slow.
I actually recommend with the first example that one disable view state for the image control.
Note that with this 2nd example, you have to create that image handler page, but you do thus remove the on row data bound code. So, a trade off here.
So, while your post SPECIFIC asked for not creating a handler?
it is a good idea – and how important to use a handler will be based on many factors, such as how many images, how big they are, and does the resulting page require post-backs. You have to pay VERY close attention to this issue of post-backs, since if you don’t, such pages can run VERY slow with a poor internet connection.
You need to use web method API that receives imageKey and write in response image file.
if use Asp.Net Form ,I suggest check Global.asx abilities.
If use Mvc , I suggest write a action that return FileResult
Now use url of web method api in your ui
code :
another way is use base64 format for display your image that I do not suggest because it makes your page heavy.