ASP.NET

Introduction to Web Forms and Scripts

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>…

2 min read547 words9 code samples

Fundamentals of Web Forms#

Creating a Form#

Adding a form to your web page is the standard way to build an interactive experience where users can submit data to you. HTML uses the <form> element for this purpose, and the following example demonstrates how to begin one:

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

<!DOCTYPE html>

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

<form></form>

</body>
</html>

A Form as a Container#

You can think of the form element as a structural container capable of holding various other HTML tags. For instance, here is a form that includes both a p and a div element inside it:

C#
<%@ 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:

C#
<%@ 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:

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:

C#
<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:

C#
<%@ 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:

C#
<%@ 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:

C#
<%@ 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:

C#
<%@ 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:

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

<!DOCTYPE html>

<html>
<head>

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

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Keep in mind that the only essential attribute is runat. Any other attributes you might include are strictly optional. Once you have defined the necessary attributes, you can proceed with writing your logic. Previous Copyright © 2009-2016, C# Key Next