Single File Mode in ASP.NET


Here I will show you step by step procedure how to create a Single File Mode Example in ASP.NET using Visual Studio 2005 or Visual Studio 2008.

What is Single File Mode Application?

In a typical ASP.NET application, you have a UI page (ASPX file) and a code behind (.cs or .vb) file. A Single File Mode application means you will write and compile your C# or VB.NET code in the ASP.NET page itself inline. There will be no code behind file.

To create this application, I am using Visual Studio 2008 but you can use Visual Studio 2005 if you are running Visual Studio 2005.


Let's try our sample.


The Steps are:

  1. Start -> All Programs -> Visual Studio 2005 or Visual Studio 2008

    Now The Visual Studio will open like this:-

     1.jpg
  2. Now go to File Menu -> New -> Web Site

    When you will click on the New Web Site a new form will open like this:-

     2.gif
  3. Under Visual Studio Installed Template-> Choose ASP.NET Web Site -> Choose File System from the location combo box -> Set the path by the browse button - > Choose the language from the Language ComboBox (Visual C# , Visual Basic , J #)

    Choose Visual C#
     
  4. Click on the OK Button:-

    Now you will see a new from like this :-

    3.gif

    This is the Source Code window and in this page you will se this code.

    First you have to learn XML and HTML to work with ASP .NET.

    I have used <"text"> to make the text as comment this is the way to make comment in this code.

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Type your webpage title here</title>  <Here you can specify your page title in between title tag>
    </head>
    <body>
    <
    form id="form1" runat="server">
    <div>
    <"In between div tags you can manage your web controls like buttons, labels, picture
    Box, ImageMap etc">
    </div>
    </
    form>
    </
    body>
    </
    html>

    See here is a tab named Design in the bottom of this page
     
  5. Click on this tab and you will see a blank web page where you can drag any control from the toolbox (which is in the left side of this window)
     
  6. Drag a Button and a Label from the Toolbox window, and add a tag in the button code like this:

    <asp:Button id="Button1"
    runat="server"
    onclick="Button1_Click" <!- -Add this line in the source window -->
    Text="Click Me!"> <! - - This is the button text -- >
    </asp:Button>

    <"Here I used a line "Welcome to ASP.NET 3.5" you can type any line here as you like -->
     
  7. Now add this code in the source window like this:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">

    void Button1_Click(Object sender, EventArgs e)
    {
    Label1.Text = "Clicked at " + DateTime.Now.ToString();
    }

    </script>
    <
    html>
    <
    head>
    <
    title>Single-File Page Model</title>
    </head>
    <
    body>
    <
    form id="Form1" runat="server">
    <div>
    <
    asp:Label id="Label1"
    runat="server"></asp:Label>
    <
    br />
    <asp:Button id="Button1"
    runat="server"
    onclick="Button1_Click"
    Text="Click Me!">
    </
    asp:Button>
    </
    div>
    </
    form>
    </
    body>
    </
    html>
     
  8. Now Run your web site by Ctrl + F5
     
  9. Click the button
     
  10. When you will click on the button the label which you have taken will show you the current date and time of your computer...
     
  11. Close web browser
     
  12. Close visual studio

Thanks


Similar Articles