help a .NET newb :)

I am very familiar with VB and ASP, but new to .NET. I am sure I will have lots of questions, but I'll start with this one:

what is the difference between putting code in <script runat=server></script> on the .aspx page and putting code on the code-behind page (aspx.vb)?

Is there any difference?  If so, is the purpose of the code-behind page just for organization or your code vs. UI elements?

Thanks for the help!
Who is Participating?
Jon500Connect With a Mentor Commented:

The best way to understand the HTML document and its code-behind module is to think of one as the presentation layer and the other as the code layer. Script tags, which are processed by the browser, will go in the aspx document while vb code will go in the aspx.vb (code-behind) document because they are compiled and processed by the .Net's CLR (common language runtime).

The "runat=server" attribute of many tags is required for that web control to fire events that can be processed by the code-behind class module. In fact, many new programmers forget this attribute, and they will soon discover that the control is not firing events. (If you drag-and-drop server controls from Visual Studio's toolbox, you will find that the runat tags are correctly set-up for you.) Also, this attribute is required in order for the code-behind class module to recognize that the control is on the web form. For example, you can place a client-side text box (one that lacks a "runat=server" attribute) on your HTML document, but you will discover that you cannot set or read its Text property from the code-behind class module.

One of the most important concepts is that the web page (aspx) document should, in theory, be something you can give to an HTML designer/graphic artist. This artist can use such tools as Dreamweaver or FrontPage to jazz-up the page without any knowledge of your code-behind contents. As long as the artist keeps your controls' tags intact and doesn't delete any of the directives that appear at the top of the .aspx document, everything will work when you use the jazzed-up version in your application.

You asked about putting code in the aspx document versus the code-behind module. The fact is that you can do either, but read on for why you shouldn't… I will often set the Visibility attribute of a panel control to this:

//C# Syntax---
//METHOD 1 (pull)
<asp:Panel runat=server id=panel1 visible=<%=GetPanel1Visibility()%>>

Notice that I'm referring to the function "GetPanel1Visibility". That is a function that I create in the code-behind class module for the page, and it returns a bool value (in C#) that is true or false.

You could have also done this in the code-behind module:

// C# Syntax---
//METHOD 2 (push)
Panel1.Visible = false;

Both will cause the Panel1's visibility to be set to false. One advantage of using Method 1 vs. Method 2 is that you can change the function name using any editor without recompiling your project. With the code-behind module approach, you must recompile before any changes will take effect.

Many programmers avoid Method 1 because the "pure" way to use .Net is to have all the server-side code in the code-behind module and only presentation-layer (HTML) and client script in the aspx document. This clean separation of the design layer and the programming logic is one of the milestones of ASP.NET over classic ASP.

I hope this helps.

-- Jonathan
jkwassonAuthor Commented:
Thanks for that answer. That is exactly what I was looking for :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.