Latest web development tutorials

ASP.NET Web Pages HTML form

Forms are HTML documents to place input controls (text boxes, check boxes, radio buttons, drop-down list) parts.


Create an HTML page input

Razor examples

<html>
<body>
@{
if (IsPost) {
string companyname = Request["companyname"];
string contactname = Request["contactname"];
<p>You entered: <br />
Company Name: @companyname <br />
Contact Name: @contactname </p>
}
else
{
<form method="post" action="">
Company Name:<br />
<input type="text" name="CompanyName" value="" /><br />
Contact Name:<br />
<input type="text" name="ContactName" value="" /><br /><br />
<input type="submit" value="Submit" class="submit" />
</form>
}
}
</body>
</html>

Running instance »


Razor example - display image

Suppose there are three images in your image folder you want to display images dynamically based on user selection.

This can be achieved by a simple piece of Razor code.

If there is a picture called "Photo1.jpg" in your site's images folder, you can use the HTML <img> element to display an image, as follows:

<img src="images/Photo1.jpg" alt="Sample" />

The following example demonstrates how to display user selected images from the following list:

Razor examples

@{
var imagePath="";
if (Request["Choice"] != null)
{imagePath="images/" + Request["Choice"];}
}
<!DOCTYPE html>
<html>
<body>
<h1>Display Images</h1>
<form method="post" action="">
I want to see:
<select name="Choice">
<option value="Photo1.jpg">Photo 1</option>
<option value="Photo2.jpg">Photo 2</option>
<option value="Photo3.jpg">Photo 3</option>
</select>
<input type="submit" value="Submit" />
@if (imagePath != "")
{
<p>
<img src=" @imagePath " alt="Sample" />
</p>
}

</form>
</body>
</html>

Running instance »

Examples explained

The server creates a variable calledimagePath.

HTML page has adrop-down list of named Choice(<select> element). It allows users according to their own will choose a name (such asPhoto 1), when the page is submitted to the Web server, pass a file name (such as Photo1.jpg).

Razor Code byRequest [ "Choice"] reads the value of Choice.If the path through the code to build the image (images / Photo1.jpg) effective, put the image path assigned to the variableimagePath.

HTML page, <img> element is used to display the image. When the page is displayed, src attribute is used to set the value of imagePath variable.

<Img> element is in a block if it is not to prevent the display image name, such as when the page is first loaded displayed.