Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help a .NET newb :)

Posted on 2005-03-31
2
Medium Priority
?
167 Views
Last Modified: 2010-04-07
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!
0
Comment
Question by:jkwasson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Jon500 earned 500 total points
ID: 13677532
Hello,

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()%>>
...
</asp:Panel>

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
0
 
LVL 5

Author Comment

by:jkwasson
ID: 13682219
Thanks for that answer. That is exactly what I was looking for :)

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question