asp.net listview newline - asp.net

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>") %>' />

Related

perfomance of .ashx handlers for retrieving a lot of binary images

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.

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 Textbox - Avoid Copy Paste of few characters

We need to stop copy paste of few characters like '<' or '>' or any characters which can be potentially dangerous.
I know we can set a property RequestValidation to false, but for some reason we would not want to do that.
What we want to do is when the user tries to paste a text in the textbox, we want to validate the text and do a pattern match against the defined list of characters to be filters.
We tried various textbox events like OnPaste (Works in IE as well as Mozilla, but in Mozilla we are not able to get the content from the clipboard), OnBlur (Works fine but does not works when clicked on checkbox with runat as Server) and few others with no real progress.
Would appreciate if anyone of you guys can help me and help me sooner as we are in deadlines!
Thanks a lot,
Atul
First of all, you really shouldn't be accessing the user's clipboard, even if some versions of IE allow it. For reasons of integrity, that's simply nothing a browser should be dealing with. Either way, you could check the textbox value after paste, rather than inspecting the clipboard content during paste.
However, better still would probably be to perform this validation on submit. Try the built in regular expression validators. Because really, you worry about users submitting dangerous characters, right, not about the fact that they actually pasted them?
EDIT (example)
<asp:TextBox ID="txtName" runat="server"/>
<asp:RegularExpressionValidator ID="regexpName" runat="server"
ErrorMessage="This expression does not validate."
ControlToValidate="txtName" Display="Dynamic"
ValidationExpression="^[^<>]$" />
Are you trying to modify what is in the users clipboard before the paste? Why not attach a onchange event that does any validation that you require? You could replace out any characters that you don't want in there.

Label Text Property and entities

The following asp label fails to be displayed in the browser, can someone please
tell me what I am doing wrong. I expect to see the value <abc> but instead
I get nothing.
<asp:Label ID="Label1" runat="server" Text="<abc>"></asp:Label>
By the way, I realize that I can accomplish the same thing doing the following:
<asp:label id="Message1" runat="server"> <abc> </asp:Label>
But that is not really what I am asking for, what I would like to know is if using a string such as "<abc>" in an attribute value for an asp elements is allowed or not. In other words, is this an ASP.Net bug or is this behavior by design and if it’s by design what’s the reason for such design?
Thank you very much.
Believe it or not, but you can include entities without escaping them, thus:
<asp:Label runat="server" ID="myLabel" Text="<abc>" />
This will render an <abc> tag.
Edit: OK, sorry, you want to display the brackets, not make a tag, of course..
Using entity references in the Text attribute will give the same result - an (invisible) <abc> tag - because they are translated when the tag is parsed server-side. What you must do is:
<asp:Label runat="server" ID="myLabel" Text="&lt;abc&gt;" />
This will give the desired result - the & entity reference will render an ampersand to the client. Followed by lt;, the result is a correct client-side entity reference (<). Which will render as <.
To answer you questions explicitly: Yes, using entity references in ASP.NET attributes is (obviously) OK, since it's an XML format. This is not really a 'decision' on Microsoft's part (and certainly not a bug) - i's simply XML.
The trick is realizing when the entity references are parsed (when the tag is parsed on the server), and what the resulting text is, which is what will be sent to the client.
Yes it's allowed of course. Label control's purpose is to show text and markup to client. And it's really useful I think. injected code is your responsibility.
The asp.net aspx parser will unescape the "<" and ">" to "<" and ">". It will generate something like this method:
[DebuggerNonUserCode]
private Label __BuildControlLabel1()
{
Label __ctrl = new Label();
base.Label1 = __ctrl;
__ctrl.ApplyStyleSheetSkin(this);
__ctrl.ID = "Label1";
__ctrl.Text = "<abc>";
return __ctrl;
}
If you wanted to write it in the text property you could double escape like "&lt;", but it is probably easier just to write it between start and end tags like you mention.
<asp:Label ...><abc></asp:Label>.

Resources