Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Archive for January, 2011

Using inline code in ASP.NET (C# and VB.Net)

Posted by Ravi Varma Thumati on January 5, 2011

Using this syntax, you can use inline code in ASP.NET (.aspx) pages. The server-side code will be automatically compiled by the .NET framework the first time the page is requested on the server. The compiled .dll file is stored in the “Temporary ASP.NET Files” system folder. Changing the code in .aspx files will trigger a new compilation, generating new .dll files. The old .dll files are phased out by the framework and eventually deleted.

C#

<%@ Import Namespace=”System” %>
<%@ Page Language=”c#”%>

<script runat=”server”>
public string ServerSideFunction(string input)
{
return “Hello ” + input;
}
</script>

<% string pageVariable = “world”; %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″ />
<title>ASP.NET inline</title>
</head>
<body>
<% =ServerSideFunction(pageVariable) %>
</body>
</html>
VB.Net

<%@ Import Namespace=”System” %>
<%@ Page Language=”VB” %>

<script runat=”server”>
Public Function ServerSideFunction(input As String) As String
ServerSideFunction = “Hello ” & input
End Function
</script>

<% Dim pageVariable As String = “world” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″ />
<title>ASP.NET inline</title>
</head>
<body>
<% =ServerSideFunction(pageVariable) %>
</body>
</html>

 

Advertisements

Posted in 1. Microsoft.NET | Tagged: | 6 Comments »