perfomance of .ashx handlers for retrieving a lot of binary images - asp.net

I used .ashx handler for getting images from database.I want to retrieve a lot of images (>1000) in this way:
<img src='GetImage.ashx?id= <%# Eval("id") %>'/>
(why I do this you can understand if read my previous question: bind database image to ItemTemplate in .ascx ).I am afraid that multipiles database querys (first query to get all id's,all others for getting image one by one) will take a lot of time,is it? What are possible solutions?

First of all the browsers did not ask the images all together, but few at a time.
Second, the handler is not use session, so its not lock the one the other and so a parallel process can be done for the image call.
The extra that I suggest to add it a cache for the browser, so when its load an image to not ask it again.
An example:
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(120));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 120, 0));
but you can add more aggressive cache.
One similar question: call aspx page to return an image randomly slow

Caching is always a good idea for static'ish content that is asked for frequently.
Also if the images are relatively small you could use data uri's http://css-tricks.com/data-uris/
Say you had an Images table with ID INT, Name VARCHAR(64), MimeType VARCHAR(64), and Data VARBINARY(MAX)
SELECT Name, 'data:' + MimeType + ';base64,' + cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:column("Data")))', 'VARCHAR(MAX)') AS DataURI
<img src='<%# Eval("DataURI") %>' alt='<%# Eval("Name") %> />
In this way you can have 1 database query that will return everything.

Related

SQL Server query + ASP.NET: Counting unique user's post(s) and then making a link to open those posts in a gridview

I was just going through in the making of a project. I am using SQL Server 2008 Expresss, ASP.NET (VB backend), .NET Framework 3.5
My question is related to :
I have a table with columns such as : userid, postid, postname, postval etc.
Now what I would like to do is to present a list of unique user's post counts.
As well as a simple navigation facility such as: a link from those unique post counts by a user. Suppose "abc" is the user.. on clicking the no. of counts of posts by abc (in a list of others) I will be redirected to another list having a gridview or any suitable tabular format for viewing those particular posts by "abc" user.
How can I achieve this..? My mind is all but confused over the query / implementation part :X
Cheers,
-[echo9]-
now what I would like to do is to
present a list of unique user's post
counts
SELECT
userid
,count(postid) as post_count
FROM
the_table
Group By
userid
Now you can bind this to a datagrid (or even better a datarepeater) with an item template that looks like:
<ItemTemplate>
<div>
<a href="myShowListPage.aspx?userid='<%# DataBinder.Eval(Container.DataItem,
"userid") %>' <%# DataBinder.Eval(Container.DataItem, "userid") %>
</div>
<div>
<%# DataBinder.Eval(Container.DataItem,
"post_count") %>' </div>
</ItemTemplate>
Now you will have to use styles and css classes to get the layout and effect you want, but this will move you in the right direction.
note: another suggestion that you learn entity framework is valuable, but may not get you moving very quickly
One solution is to use Entity Framework ..
Read about it

CKEditor breaking custom .NET tags by converting single quotes to double quotes

At the client's request, we just upgraded a custom CMS system for a large site from FCKEditor 2.x to CKEditor 3.5.3.
Inside an ItemTemplate I have a custom UserControl tag in which the attributes are populated by DataBinding, like so:
<my:Viewer runat="server">
<ItemTemplate>
<my:CustomTag runat="server"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>' />
</ItemTemplate>
</my:Viewer>
So, the point is that the above works just fine. However, when the HTML is put into the latest CKEditor, CKEditor changes the ImageUrl attribute to use double-quotes instead of single quotes. Once it's changed to double quotes, it causes a parsing error on the .aspx page. Changing: "ImageUrl" to "ImageUrl" works, but it's not ideal for our client who is going to have to update every page that exists in a very large CMS system. So, I'm asking this question hoping someone might know of a way to toggle CKEditor to use single quotes in HTML attributes by default instead of double quotes to reduce the amount of work my client is going to have to do.
I'm only looking for easy configuration-type changes, not patching the editor, etc.
This should do what you want
Taken from here
http://cksource.com/forums/viewtopic.php?f=11&t=20647&sid=f47526ecfb1f2303ad0b923ceed7aafe&start=10
To avoid CKEditor changing special chars:
switching in source view:
CKEDITOR.instances.TEXT.on( 'mode', function(ev) {
if ( ev.editor.mode == 'source' ) {
var str=ev.editor.getData();
str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
ev.editor.textarea.setValue(str);
}
});
When save edited document:
var html=CKEDITOR.instances.TEXT.getData()
html=html.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
I'm going to say that the " solution that I mentioned being too much work is simply the only answer...just to put some closure on this. Or, if I can find a way, I'll withdraw the question. Thanks rqmedes for trying...I'd actually forgotten all about this question until I got your response
:)

showing images through gridview?

suppose my images binary data is saved in database table and i want to show those images through gridview. one approach is just create one page where i will pass some id as query string and that page will retrieve the image data from db and stream down the image data to my page through BinaryWrite method. a example is http://www.aspdotnetcodes.com/Insert_Images_Database.aspx. this url describe how to show image data through BinaryWrite.
this approach is old and i know. is there any other way by which i can show the image in gridview but i don't want to store image url in table.please let me know if you know any other approach for showing the images through gridiview when image binary data is stored in db. thanks
I asked a similar question here:
Streaming Databased Images Using HttpHandler
Note that I had to use a DataReader in my final version :-)
It sounds like you've got your images stored in your database in binary format. You then want to show them in your GridView.
Consider this approach:
create a template field in your gridview to hold your image. It'll end up calling a handler URL to grab the binary data for your image.
create the image handler page -- make it an .ashx. Put a unique identifier on the querystring, for example, to grab the image you want from the database
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="80px" Width="80px"
ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
See the answer at the bottom of this MSDN question for more details on how to implement your image handler. You then won't have to bring back the binary data when binding your GridView. On the other hand, it creates n calls to the handler for each row in the grid.

asp.net listview newline

I'm pulling data out of a database with a listview on a webpage.
When I was working with my excel source before transferring to database I was told vbcrlf's will automatically get rendered. This is my first app now that all my data is showing without the line breaks and I was told from someone else I'm supposed to have br tags instead of the vbcrlf's.
To avoid any future trouble, do any of the other data controls render vbcrlf's or is this just a listview peculiarity in ASP.net?? If not, how do I make my webpage render the vbcrlf's? If so, whats the simplest way to replace vbcrlf's for br tags in a mysql database? (/w utf8 char set)
Thanks
That does it
<asp:Label ID="DetailsLabel" runat="server" Text='<%# Eval("Details").Replace(Environment.NewLine, "<br>") %>' />

Explicit localization problem

when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.

Resources