Re: Question about separating logic from html?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Kevin Spencer (kspencer_at_takempis.com)
Date: 05/06/04


Date: Thu, 6 May 2004 09:26:18 -0400


> For what i know of asp.net, it should be possible to query my database
> in the upper side of the aspx-file (logic) and put the hyperlinks at
> the bottom of the page (html). Could this be separated or do i have
> to mix asp.net-code and html code (spaghetti)?

Hi Michel,

When you speak of "the upper side of the aspx-file" and "the bottom of the
page (html)" you are thinking in classic ASP terms, which means
procedurally. In classic ASP, code is executed from top to bottom of the
page/script, which contains both server-side scripting, and HTML code.

ASP.Net is Object-Oriented, which means (among other things) that there is
no procedural order in which code is executed. Instead, objects are created,
and the execution thread is much more complex, passing among objects in a
non-linear fashion. It is more useful to think in terms of objects than
procedure with ASP.Net.

You can use a separate CodeBehind file, or put your CodeBehind code into the
ASPX Page Template in code blocks if you like. However, it is best to think
of the CodeBehind not as "the upper side of the aspx file," but rather as a
separate Class definition which can be optionally included in the same file
as the ASPX Page Template.

ASP.Net uses objects (Server Controls) to render HTML in the page. Any "raw"
HTML in the ASPX Page Template is converted at run-time to LiteralControls.
Other Server Controls are used for client-server interaction. These other
Controls have a server-side and a client-side aspect to them, and the
client-side aspect "talks to" the server-side aspect by means of form
PostBacks, since they don't "live" at the same time (the server-side objects
render the client-side HTML, and then go away until the next request. The
client-side HTML exists until a new Request is sent to the server).

Server Controls often do have a representation in the ASPX Page Template.
This representation in the Template file itself look similar to HTML or XML,
but at run-time they are rendered to the client as pure HTML. The HTML that
is rendered in the HTML document, however, looks nothing like the
server-side representation of the Control in the Template. Instead, it is
created by the server-side object (which is in the CodeBehind class of the
page) that is the "real" Server Control. The token in the Page Template is a
sort of placeholder, which can contain some code that builds the HTML
rendered at run-time, by combining information in the Server Control tag in
the Page Template with logic from the Server Control behind it.

In essence, this is how the Presentation tier of an ASP.Net app is divided
into presentation logic (Server Controls in the CodeBehind) and presentation
layout (the pseudo-HTML of Server Controls and "raw" HTML in the Page
Template).

I hope this clears things up for you somewhat.

-- 
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"mca" <m_cambeen@hotmail-dot-com.no-spam.invalid> wrote in message
news:409a0dc5$1_1@127.0.0.1...
> Hi everyone,
>
> I'm new to asp.net and i have a question about separating the html
> code from the programming code.
>
> i have an unknown numbers of entries in my table. I want to make a
> hyperlink for every entry in my table. So i query the database and
> get for example 3 entries back. So in a while loop i can make 3
> hyperlinks with response.write(.......) etc.
>
> For what i know of asp.net, it should be possible to query my database
> in the upper side of the aspx-file (logic) and put the hyperlinks at
> the bottom of the page (html). Could this be separated or do i have
> to mix asp.net-code and html code (spaghetti)?
>
> Anyone?
>
> Regards,
> Michel
>


Relevant Pages

  • Re: ASPX now with methods and classes
    ... > - color coding of html and javascript and dhtml dom ... > - volatile storage (session object) ... >> ASP.NET, from the Page class to all of the server controls, is nothing ... >> but a bunch of rendering libraries. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ASPX now with methods and classes
    ... - color coding of html and javascript and dhtml dom ... - volatile storage (session object) ... > ASP.NET, from the Page class to all of the server controls, is nothing ... > but a bunch of rendering libraries. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How do you inspect HTML controls in compiled C#?
    ... you say you "put a server control on my page that internally renders ... ASP.Net is all about HTML and HTTP. ... So, in fact, an ASP.Net application has both a server-side and a client-side ... The built-in Microsoft Server Controls, as well as any custom Server ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Converting HTML to ASPX
    ... some considerable time. ... way that happens is the one person builds HTML pages (usually first) and ... convert a HTML file to an aspx/codebehind pair. ... server controls mapping to them, you can add a runat="server" attribute ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: asp:TableCell & Background image
    ... the "the original architects ... ASP.NET has provided the HtmlControls which are mapped to the original html ... html element in the HtmlView at design time. ... #The Forgotten Controls: HTML Server Controls ...
    (microsoft.public.dotnet.framework.aspnet)