Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Instroduction
 

Code-behind model allows for code separation of the pages business logic from its presentation logic

presentation logic  for the page is stored in an .aspx page, whereas the logic piece is stored in a separate  class file: .aspx.vb or .aspx.cs. File Options Using Code-Behind                File Created Web Form                                      .aspx file                                               .aspx.vb or .aspx.cs file  Master Page                                    .master file                                               .master.vb or .master.cs file  Web User Control                            .ascx file                                               .ascx.vb or .ascx.cs file  Web Service                                    .asmx file                                               .asmx.vb or .asmx.cs file  An .aspx page that uses the ASP.NET 2.0 code-behind model (VB version) <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"    Inherits="_Default" %>      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">      <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">    <title>Simple Page</title> </head> <body>    <form runat="server">       What is your name?<br />       <asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />       <asp:Button ID="Button1" Runat="server" Text="Submit"        OnClick="Button1_Click" />       <asp:Label ID="Label1" Runat="server"></asp:Label>    </form> </body> </html> A code-behind page  (VB version) Partial Class _Default    Inherits System.Web.UI.Page         Protected Sub Button1_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Button1.Click             Label1.Text = "Hello " & TextBox1.Text    End Sub End Class An .aspx page that uses the ASP.NET 2.0 code-behind model (C# version)   <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_aspx" %>        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">      <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">    <title>Simple Page</title> </head> <body>    <form runat="server">       What is your name?<br />       <asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />       <asp:Button ID="Button1" Runat="server" Text="Submit"        OnClick="Button1_Click" />       <asp:Label ID="Label1" Runat="server"></asp:Label>    </form> </body> </html> A code-behind page (C# version) using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;      public partial class _Default : System.Web.UI.Page {    protected void Button1_Click(object sender, EventArgs e)    {       Label1.Text = "Hello " + Textbox1.Text;    } }