Code behind page sample

Please let other users know how useful this tip is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.

Code behind is a new feature of ASP.NET that lets you separate code and presentation in different files. With this kind of technique you could write a presentation in an .aspx file and the code could be any .NET-compatible language (C#, VB.NET, etc.). The benefit is that it becomes easy to manage code and the presentation of it, while the work can be distributed -- people good at presentation can work on that part of the application and the programmer could work just on the coding part.

There are actually two physical files on the disk where a tag in .aspx files tells the name of the class and the name of the physical file. Note: Inherit tells the name of the class in the code behind file and SCR is source file. When the default.aspx loads, you see that page_load event is called in the default.vb file, and, similarly, on click the button we see the code gets called from the default.vb file.


<%@Page language="vb" inherits="CodeBehindClass"
src="Default.vb" %>
<form runat="server">
<asp:button id="button1" text="button 1" onclick="button1_click"
runat="server" />
<asp:label id="lbl" text="label and button in the aspx file " 
runat="server" />


imports system
imports system.web
imports system.web.ui
imports system.web.ui.webcontrols
imports system.web.ui.htmlcontrols
      public class CodeBehindClass 
      Inherits System.web.ui.page
             public lbl as label
             sub Page_Load(sender as object,e as eventargs)
                        lbl.Text = "page load in Code Behind" 
             end sub
             sub Button1_click(sender as object,e as eventargs)
                     lbl.text = "Button click in Code Behind"
              end sub
       end class

Save both the pages to wwwroot and run in Default.aspx in the browser.

This was first published in June 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.