1

I must not understand something fundamental about the aspx page processing cycle. Please take a look at this simple example below.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title></title>
</head>

<body>
    <div>
        <form method="post">
            <textarea name="someContent" cols="35" rows="15"></textarea>
            <input type="submit"/>
        </form>
    </div>
</body>
</html>



<script runat="server">
    public void Page_Load() {
        // The httpMethod is always set correctly to "GET" or "POST" 
        String httpMethod =  HttpContext.Current.Request.HttpMethod;

        if(IsPostBack)
            DoSomething();
        else
            DoSomethingElse();  
    } 
</script>

Note the <form> element does not have a runat='server' attribute.

When the page is loaded for the first time, the Page_Load() fires and the httpMethod variable is set to "GET" and the IsPostback property returns false, all as expected.

When the user clicks the "submit" button, the Page_Load() fires again and the httpMethod variable is set to "POST", so the ASP.NET plumbing obviously knows this is a POST verb; however, the IsPostBack property still returns false. This seems odd to me. I would think that if the httpMethod was set to "POST", the IsPostBack would return true.

If I change the <form> element to contain a runat='server' attribute things change a bit. Now when the user presses the "submit" button the httpMethod variable is set to "POST", just as before, but now IsPostBack returns true.

Since I have no need to access the <form> element on the server, I saw no need to use a runat='server' attribute on it. But for some reason, the runat='server' must be present on the <form> in order for the IsPostBack to return a correct value, even though the HttpContext.Current.Request.HttpMethod property returns the correct value regardless of the runat='server' attribute.

Can anyone explain WHY the runat='server' is necessary on the <form> to make IsPostBack work correctly?

NOTE: Please note that I am not asking how to "do this" or "do that". My goal is to understand "The Why".

Thanks

Tom Baxter
  • 2,110
  • 2
  • 19
  • 38

1 Answers1

0

Page checks few special fields (viewstate and postback event) to determine if request is postback or not.
http://referencesource.microsoft.com/System.Web/R/ae07c23d0aba6bb9.html

Both forms here cause IsPostBack to be true:

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>

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

        <p><%= IsPostBack? "POSTBACK" : "NO POSTBACK" %></p>

        <form id="form1" runat="server">
            <input type="submit" />
        </form>

        <form id="form2" method="get">
            <input type="hidden" name="__EVENTTARGET" value="" />
            <input type="submit" />
        </form>

    </body>
</html>

Server-controlled form just adds and populates these fields automatically.

Leonid
  • 3,121
  • 24
  • 31