Home

Introduction to Web Forms and Scripts

Fundamentals of Web Forms

Creating a Form

To create an interactive web page that allows visitors to send values to you, you can add a form to it. To support forms, HTML provides the <form> element. Here is an example that starts a form:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title></title>
</head>
<body>

<form></form>

</body>
</html>

A Form as a Container

The form element is a container in which you can put any type of HTML element. Here is an example that contains a p and a div elements:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Geometry: Square</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  200pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>

</head>
<body>

<form>
  <div id="formulation">
    <p id="main-title">Geometry: Square</p>
  </div>
</form>

</body>
</html>

You can also include scripting code in a form element. Here is an example:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Geometry: Square</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  200pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>

</head>
<body>

<form>
  <div id="formulation">
    <p id="main-title">Geometry: Square</p>

    <% Response.Write("<p>A square is a flat geometric figure made of four equal sides and four right angles.</p>"); %>
  </div>
</form>

</body>
</html>

This would produce:

Fundamentals of Web Forms

Client and Server Processing

When creating an ASP.NET webpage, you must specify where its value(s) would be processed. This is done by adding an attribute named Runat (or runat) to a start tag. It can have one of two values: client or server. Here is an example:

<asp:Button runat="server"></asp:Button>

This attribute and its value must be applied to the form element. The attribute should also be added to various types of elements, including the head. Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>

<head runat="server">

<title>Geometry: Square</title>

</head>
<body>

<form runat="server">

</form>

</body>
</html>
 
 
 
     

Scripts

Introduction

There are various types of places where you will write your code. As mentioned already, you can create delimiting sections where you would write your code. Some other types of code will require that you create your code in the head section. To write ASP.NET code in the head section, you must create a script.

Creating a Script

To get a script in the head section, start a <script> tag and close it with an end </script> tag. Here is an example:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>

<script>

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

The <script> tag uses various attributes. To start, you must specify that the script will run on the server. To do this, add the runat="server" attribute to it. This can be done as follows:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>

<script runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

After doing this, you can create your code between the starting <script> and the end </script> tags.

When writing your code, because you have determined that the page will use the C# language, you can simply write your code as you see fit. Still, you have the option of choosing among various languages. The available languages are C#, VB, VBSscript, JavaScript, JScript, or ECMAScript. To let you specify the language, the <script> tag is equipped with an attribute named language. To specify the language, assign it to the language attribute. Here is an example:

<%@ Page Language="C#" %>

<!DOCTYPE html>


<html>
<head>

<script language="C#" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

If you don't specify the language, C# is assumed. Besides the language, you can specify how the code of your script will be formatted or considered. To support this, the <script> tag is equipped with the type attribute. To specify it, assign:

  • text/C# to the type attribute if you will use the C# language
  • text/VB to the type attribute if you will use the VB language
  • text/vbsscript to the type attribute if you use the VBScript language
  • text/javascript to the type attribute if you will use the JavaScript language
  • text/jscript to the type attribute if you will use the JScript language
  • text/ecmascript to the type attribute if you will use ECMAScript

Here is an example:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>

<script language="C#" type="text/C#" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Remember that only the runat attribute is required. The others are optional. After specifying the values of the desired attributes, you can create your code.

 
 
   
 

Previous Copyright © 2009-2016, C# Key Next