Asp.Net 4 Getting back control over your Html: Classic HTML form, postback and cross postback

In Asp.net there are a few different ways of posting your form for further processing.

Postback

The postback mechanism is the main pillar of Asp.net. so I won't dwell to much here. In summary, we have a server side form with an event handler on the code behind. All the form values are accessible by accessing the server side object models like Label, TextBox, DropDownList, etc. Afterwards you either 302 redirect the user to some other page (by doing a Response.Redirect) or just simply stay on the same page.

Remember that response.redirect causes an extra round trip from the server to the browser (server needs to tell the browser to redirect to the next page).

Cross Postback

If you want to use postback and avoid the extra round trip for redirection, you could use cross postback. By all means this is not a new feature. It was first released in Asp.net 2.0. However I still often find people rely on Response.redirect rather than this method.

<form id="form1" runat="server">
  <input id="personNameInput" runat="server" />
  <asp:Button PostBackUrl="~/Action.aspx" Text="submit" runat="server" />
</form>

Source.aspx : server side form with a bunch of asp.net controls

public string Name { get { return personNameInput.Value; } }

Source.aspx.cs : expose the values as a public property so that it is accessible on the target page.

protected void Page_Load(object sender, EventArgs e)
{
  if (PreviousPage == null) return;
  Name = PreviousPage.Name;
}

Target.aspx : Access the public properties of the source page through the PreviousPage property.

Read more here: Cross-Page Posting in Asp.net web pages

Unfortunately the Html markup that Asp.net produced rely on javascript to cross post the form to the other page and not rely on the action property of the html form element.

Plain old Html

As you dig deeper into Asp.Net, you will learn that you don't have to do everything the Asp.Net way. Asp.net are more than capable of handling plain old HTML submit. Just to refresh, there are two properties in a form element that you'd be interested in; method : whether you want the values to be embedded in the query strings (GET) or in the post (POST), and Target : where you want to send the values to.

<form id="form" action="action.aspx">
  <input name="personsNameInput" />
  <input type="submit" value="submit" />
</form>

Source.html : plain old Html Form

On the target page, all we have to do to get the values of the form is through the Request object.

protected void Page_Load(object sender, EventArgs e)
{
   Name = Request["personsNameInput"];
}

Target.aspx.cs : target page code behind

Of course the source page could be dynamic asp.net pages, but the bare minimum is as shown above. This is the cleanest way of getting a value for server side processing.

In Summary

Even when you're using Asp.Net, it doesn't mean that you have to implement everything using the rich Asp.net features. Using html Form allows you to have clean control over your markup, allowing you to do stuff like; have more than 1 forms on a page, posting to an https without having the source page hosted on https, full control over the ’หœenter' key, etc. Stuff that may be simple, but just straight up hard to do on a canonical Asp.net webform page.

Especially coupled with the new cutie bee sting <%: (as oppose to the old <%= or an actual  image ), and the new clean Html ID that Asp.Net 4 provides, you'd get really close to Asp.net MVC in terms of getting control back over your html.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>target</title>
</head>
<body>
    <div>
        <%: Name %>
    </div>
</body>
</html>

Target.aspx : doesn't use much Asp.net server controls.

  1. Prakash says:

    Hi I have an application which has form without runat=server and login html5 tags and in body section im using accordian of ajax control toolkit so section requires runat=server. Now please help me in this. I want to have server code logintosystem() for click on login submit for login_form which has no runat. so how to reach to that code. answer to this issue Or any way i can know in login_form the submit button was clicked by user which caused the form to submit. Thanks and waiting for reply as low time to complete the application

  2. ronaldwidha says:

    Hi Prakash, this is a classic case of using cross postback as what I explained in the above post. Wrap your login tag inside a form tag (without runat server) and point it to a separate page login.aspx. In there on page load, you take the values from the textboxes by Request.Form["id"]. Good luck!

  3. Charles Horton says:

    I am very interested in your post about .NET Development. It is more useful and helpful for the Software developers to make the applications. Thanks for share this valuable post.