ASP.NET Code
  Home arrow ASP.NET Code arrow Page 5 - ASP.NET Basics (part 1): Nothing But .Net
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

ASP.NET Basics (part 1): Nothing But .Net
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 119
    2003-07-23

    Table of Contents:
  • ASP.NET Basics (part 1): Nothing But .Net
  • Checking Out The Menu
  • Building Blocks
  • Hard Choices
  • Command And Control
  • Naming Names
  • Linking Out
  • Putting The Pieces Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    ASP.NET Basics (part 1): Nothing But .Net - Command And Control
    (Page 5 of 8 )

    One bane of ASP programming was the "spaghetti" style of coding, in which HTML and in-line ASP code were often fighting for space and attention. More often than not, maintaining such code was a nightmarish experience, and one best handled after a few strong cups of coffee.

    ASP.NET provides a more elegant solution in the form of "server controls". Take a look:


    <script language="C#" runat="server">
    void Page_Load()

    quote.Text="Choice. The problem is choice.";

    </script>
    <html>
    <head>
    <title>Hello, Choice!</title>
    </head>
    <body>
    <asp:label id="quote" runat="server" />
    </body>
    </html>

    Save this as "label.aspx" in your "ASP101" folder and view it in your browser - you should see something like this:

    The example above is much easier to read compared to the earlier one, because I've managed to separate the dynamic C# code from the static HTML tags, thereby making it easier to comprehend (and easier to maintain in the long run).


    <script language="C#" runat="server">
    // snip
    </script>

    If you're familiar with JavaScript, you already know about the <script> element, which makes it possible to embed JavaScript client-side code within an HTML file. ASP.NET takes this one step further and allows the developer to embed server-side code within an HTML page, as seen in the above code snippet.

    The "runat" attribute within the <script> element, which contains the value "server", indicates that the code within the <script> element should be executed on the server and not the client. As earlier, I have to indicate the language using the "language" attribute.

    Don't worry too much about the code within the <script> element at the moment, I'll be coming back to it in a moment. Instead, proceed downwards, to the second unfamiliar element in the page:


    <asp:label id="quote" runat="server" />

    In ASP.NET-lingo, this is called a "server control". Server controls are very similar to the regular HTML elements your normally use, with the main difference being that they are actually created at run-time on the server.

    Confused? Take a look at the source code of the page generated by the example above - you'll see that ASP.NET automatically converts the server control into an equivalent <span>:

    [output]...[/output]

    Note that the "id" attribute in the rendered version has the same name as the "id" attribute given to the server control in the ASP.NET source code. This "id" is a unique identifier that can be used to refer to the server control in your C# code...as I have done a few lines above:


    <script language="C#" runat="server">
    void Page_Load()

    quote.Text="Choice. The problem is choice.";

    </script>

    The code within the Page_Load() function is automatically executed by the Web server when the page is loaded. In this particular example, the code sets the "Text" attribute of the server control named "quote" to the line of text "Choice. The problem is choice."

    When the server loads the page, it looks for a server control named "quote" within the page and, on finding it, sets its "Text" attribute to the value specified. The server control is automatically converted in an equivalent HTML element - a <span> - and the value of the "Text" attribute is written inside it.

    A number of other attributes of the "label" server control are also available - drop by http://www.gotdotnet.com/quickstart/aspplus/samples/classbrowser/cs/classbro
    wser.aspx?namespace=System.Web.UI.WebControls for a complete list

    Server controls are an important aspect of programming in ASP.NET. I'll be exploring them in-depth as we move along, I've only included the example above to give you an idea of how they can be used.

    More ASP.NET Code Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway