HTML and CSS Reference
In-Depth Information
<br /><br />
<input type="submit" name="Button1" value="Submit" id="Button1" />
As you can see, at runtime ASP.NET adds several pieces to the resulting HTML markup. Notice now the
<form> tag has action and method attributes. Also notice that all the server controls are converted into their
equivalent HTML markup. Finally, note the ViewState hidden form fields added at the top of the form. This
bulky Base64-encoded ViewState (truncated in the listing for the sake of clarity) is one of the reservations
ASP.NET developers have against Web Forms.
The following are a few things worth noting about the <form> tag used in a web form:
• Typically, a web form has only one <form> tag, although HTML doesn't impose any
such restriction.
• A form method is always POST .
• A web form is posted to itself. That is, the action attribute of the <form> tag points to
the same .aspx file.
• When you use master pages, the <form> tag is placed in the master page file
( .master ) rather than individual content pages ( .aspx ).
The <form> Element in ASP.NET MVC
The <form> element in the context of an ASP.NET MVC application is quite flexible. ASP.NET MVC
applications have a couple of options when you place a <form> element in a view:
• An HTML <form> tag and HTML <input> tags can be placed directly in a view file.
• HTML helpers can be used to render <form> and <input> tags.
Listing 5-3 shows a form developed using plain HTML markup tags.
Listing 5-3. A <form> in an MVC View Using Plain HTML Tags
<form method="post" action="/Home/Index">
<span id="Label1">Enter your Name : </span>
<br />
<input name="TextBox1" type="text" id="TextBox1"/>
<br /><br />
<input type="submit" name="Button1" value="Submit" id="Button1" />
This <form> uses plain HTML markup tags such as <form> and <input> . Notice that the action attribute
of the <form> tag points to a controller action method ( /Home/Index ); you're free to point it to any controller
Listing 5-4 shows the same form developed using HTML helpers.
Listing 5-4. A <form> Rendered Using HTML Helpers
<% using(Html.BeginForm("IndexWithHelpers","Home","POST")) %>
<% { %>
<%= Html.Label("Enter your Name :") %>
<br />
<%= Html.TextBox("txtName") %>
Search WWH ::

Custom Search