The things I learnt while I migrated from classic asp to asp.net is given here for my reference as well as for new beginners.

Your First Csharp Code

Learn ASP.NET(C#) By Examples

@ Nora Lamens 2009

Lesson-1

Save the following code as Ex01.aspx and run it in Browser.

<%@ Page Language="c#"%>
<html>
<head>
<title>Ex-01</title>
</head>
<body>
Welcome Raja
</body>
</html>

What you have learnt in this Lesson?

  • The word :Ex-01" is displayed on the Blue color TitleBar of Browser.
  • The word "Welcome Raja" is displayed in the body of the Browser.
  • This aspx file contains zero gram of c# code.
 

Lesson-2

Let us slightly modify the above code in our Lesson-2 and check the result:

<%@ Import Namespace="System" %>

<%@ Page Language="c#"%>
<html>
<head>
<title>Ex-01</title>
</head>
<body>
<%
string x = "Raja";
Response.Write("Welcome " + x);
%>

</body>
</html>

 

 

 

 

What you have learnt?

  • The Method Response.Write() is used to display values in the body.
  • "+" sign is used for concatenating text and variables. (VB uses "&" for concatenation)
  • Csharp code has been written between this tag : "<%.....................%>"
  • Each statement in Csharp ends with a Semicolon.
 

Lesson 3:

The code in Lesson-2 can be further complicated by introducing FUNCTIONS..

<%@ Import Namespace="System" %>
<%@ Page Language="c#"%>
<script runat="server">
public string myFunc(string x)
{
return "Welcome " + x;
}
</script>

<html>
<head>
<title>Ex-01</title>
</head>
<body>
<% Response.write(myFunc("Raja")) %>
</body>
</html>

 

 

Lesson 4:

In this lesson, we do nothing. We have simply replaced the Response.Write() by "=" short code.

<%@ Import Namespace="System" %>
<%@ Page Language="c#"%>
<script runat="server">
public string myFunc(string x)
{
return "Welcome " + x;
}
</script>
<html>
<head>
<title>Ex-01</title>
</head>
<body>
<%=myFunc("Raja") %>
</body>
</html>

 

Lesson 5:

Let us further complicate our code by creating METHODS inside CLASSES and keep these classes in a separate file called as CODE-BEHIND FILE.When we use code-behind, there will be TWO files. One is aspx file and another one is aspx.cs file

Code for Ex01.aspx file:

<%@ Page Language="C#" CodeFile="Ex01.aspx.cs" Inherits="Ex01" %>
<html>
<head>
<title>Ex-01</title>
</head>
<body>

</body>
</html>
Code for Ex01.aspx.cs file:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ex01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myMethod("Raja");
}

protected void myMethod(string x)
{
Response.Write("Welecome " + x);
}
}

The output of all the five lessons are same. you will get " Welcome Raja".

 

Thanks for Your Visit

Google Search
Disclaimer and Copy Right