C#/ASP.NET question with databases and HTML

VinylxScratches

Golden Member
Feb 2, 2009
1,666
0
0
So how would I get a page to display a query like this.


Say I have a Article.aspx?=123

In this article, I have a bunch of text and a tags like this.


[img]www.webste.com/uploads/image.jpg
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae lorem elit. Nunc at tortor tristique ante auctor lobortis in sit amet leo. Vestibulum orci leo, viverra nec volutpat feugiat, congue vitae erat. Maecenas ut arcu risus. Quisque vulputate leo nibh, ullamcorper facilisis velit. Etiam elementum ipsum a odio faucibus sollicitudin. Pellentesque lorem odio, bibendum vitae interdum sed, semper non dolor. In sodales malesuada sem, tincidunt auctor enim suscipit non. Phasellus cursus dapibus diam, non congue ligula dignissim id. Suspendisse aliquam est iaculis urna luctus non pretium ipsum ultrices. Donec hendrerit, dolor ac aliquam imperdiet, nisl elit blandit arcu, a accumsan odio arcu ut orci. Phasellus vel dui sit amet ante tincidunt laoreet et vel magna. Nullam id auctor lectus. Vestibulum velit felis, ullamcorper in ornare id, vulputate volutpat risus. Etiam a lorem at sapien condimentum varius id vel quam. Pellentesque ac augue ut nisl cursus sagittis nec vitae odio. Nam nec ipsum sapien, a ornare risus. Vestibulum interdum viverra leo non malesuada. In laoreet accumsan est nec ultricies.

How would I logically get that
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
You need to replace the img tag with the equivalent html tag (<img src="www.webste.com/uploads/image.jpg" alt="picture" /> ), how you do that is up to you! One of the more obvious ways would be using regular expressions/string replacing.
 

VinylxScratches

Golden Member
Feb 2, 2009
1,666
0
0
Alright, but how does the page "know" it's html code and not just a string read from the database? Say I do convert it to <img src="www.webste.com/uploads/image.jpg" alt="picture" />. The query will still show up as

<img src="www.webste.com/uploads/image.jpg" alt="picture" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae lorem elit. Nunc at tortor tristique ante auctor lobortis in sit amet leo. Vestibulum orci leo, viverra nec volutpat feugiat, congue vitae erat. Maecenas ut arcu risus. Quisque vulputate leo nibh, ullamcorper facilisis velit. Etiam elementum ipsum a odio faucibus sollicitudin. Pellentesque lorem odio, bibendum vitae interdum sed, semper non dolor. In sodales malesuada sem, tincidunt auctor enim suscipit non. Phasellus cursus dapibus diam, non congue ligula dignissim id. Suspendisse aliquam est iaculis urna luctus non pretium ipsum ultrices. Donec hendrerit, dolor ac aliquam imperdiet, nisl elit blandit arcu, a accumsan odio arcu ut orci. Phasellus vel dui sit amet ante tincidunt laoreet et vel magna. Nullam id auctor lectus. Vestibulum velit felis, ullamcorper in ornare id, vulputate volutpat risus. Etiam a lorem at sapien condimentum varius id vel quam. Pellentesque ac augue ut nisl cursus sagittis nec vitae odio. Nam nec ipsum sapien, a ornare risus. Vestibulum interdum viverra leo non malesuada. In laoreet accumsan est nec ultricies.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
A page of HTML is displayed by a browser that can parse and render it, so if you return the correct HTML from the database query and write it out to the response stream from an ASP.NET script, then the browser will decode and render it correctly. Of course, that's a fairly inflexible and inefficient way to generate a page from data. More typically you use ASP.NET to query values from the database and substitute those values into an existing HTML template (the .ASPX or .ASCX part of an ASP.NET page).
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
There are three ways of programming for your example:

Article.aspx with inline (script language="c#")
Article.aspx with codefile (Article.aspx.cs)
Article.aspx with assembly

I'm assuming that it's going to be one of the first two for simplicity. Article.aspx will likely have html code and asp:controls, how you deal with the logic depends if you are using a separate code file where you have to compile or using inline code but that's another matter.

You would typically write your code to process the request starting from the page_load event. You requested article 123, so you would capture than information from the request variables then you would do your db query/manipulation etc, before finally writing the output to an asp:control within the page (or write out full html if you wish). When the render method is finally called for the page (something done internally) a response stream to send back to the browser that requested is built it and by default unless you change it the default response content type is text/html.

Simplified:
User requests a page
Browser sends request to server
Server processes request
Server sends response back to browser
Browser deals with response
Browser renders page according to what it was passed

The browser doesn't know or care how the data sent back is built, as long as it has a content type and actual data, it renders it accordingly.
 

invidia

Platinum Member
Oct 8, 2006
2,151
1
0
You have to encode it to HTML. Google on how to encode a string to html for C#/ASP.NET.

I guess you can store "<img src="www.webste.com/uploads/image.jpg" alt="picture" /> " as a string and use the html encode method? I did this before from SQL data with long text + html tags, but it was under VB and a different method. The page couldn't recognize the HTML tags unless it was encoded.