6

Why do the following result in a true if clause even though the textbox is empty and not even touched on a postback? :

<form action="Default.aspx" runat="server" method="post" id="newform">
<input type="text" id="name" runat="server"/>
</form>

<%
if (Request.Form["name"] != null) // Prints out "Name OK" on postback.
{
    Response.Write("<br/>");
    Response.Write("Name OK");
}
%>

Does the textbox actually contain an empty string ("") on a postback?


Why do the following result in a true if clause on the first page load but not on a postback? :

<form action="Default.aspx" runat="server" method="post" id="newform">
<input type="text" id="name" runat="server"/>
</form>

<%
if (Request.Form["name"] != "") // Prints out "Name OK" on first page load, but not on postback.
{
    Response.Write("<br/>");
    Response.Write("Name OK");
}
%>

To get a successful and expected result I have to use the following:

<form action="Default.aspx" runat="server" method="post" id="newform">
<input type="text" id="name" runat="server"/>
</form>

<%
if (Request.Form["name"] != null && Request.Form["name"] != "")
{
    Response.Write("<br/>");
    Response.Write("Name OK");
}
%>
Heinzi
  • 167,459
  • 57
  • 363
  • 519
Birdman
  • 5,244
  • 11
  • 44
  • 65
  • disable auto friendly URL in asp.net 4.5 http://stackoverflow.com/a/40650127/184572 – Iman Nov 17 '16 at 08:42

4 Answers4

10

First, let me answer your question:

The first page load is a GET, postbacks are a POST (hence the name postback). Request.Form is populated only if the page is loaded though a form POST.

  • On the first page load, Request.Form is an empty collection. Since Request.Form is a NameValueCollection, accessing a non-existent entry returns null. Thus, Request.Form["whatever"] returns null on the first page load.

  • After a postback, Request.Form is filled with values. Since HTTP POST does not know about null values, Request.Form["whatever"] returns an empty string for fields which are present but empty.

If you want to avoid the x != null && x != "" pattern, use String.IsNullOrEmpty or the null coalescing operator: (x ?? "") != "".


On the other hand, you could make your life a lot easier by just using the built-in WebForms features instead of parsing Request.Form yourself:

<form runat="server">
    <asp:TextBox ID="nameBox" runat="server" />
    <asp:Button Text="Do Postback" runat="server" />
</form>

<%
    if (nameBox.Text != "")
    {
        %><br />Name OK<%
    }
%>

Since TextBox.Text defaults to "", there's no need to check for null here.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thank you for answering my question, but the answer not concerning my question/problem was unnecessary since I already know about the built-in WebForms features (That was also the reason why I didn't include the WebForms tag in the first place, but had to later on) You may be wondering why I'm not using MVC instead then, but there's an easy answer to that: Learning HTML/javascript from scratch + MONO. – Birdman Jan 04 '12 at 09:47
  • @Alex: Ah, ok, I understand. I'll leave the second part there anyway, just in case it might be helpful to someone reading the question in the future. – Heinzi Jan 04 '12 at 12:43
2

Request.Form["ControlName"] returns null if Control is not present on form.

If Control is present, but it contains null or empty value, then Request.Form["ControlName"] will always return String.Empty.

So it's good practice, instead of comparing (Request.Form["ControlName"] != null), use (!String.IsNullOrEmpty(Request.Form["ControlName"]))

NSNoob
  • 5,548
  • 6
  • 41
  • 54
2

Request.Form is NameValueCollection, which returns null if specified key is not found, returns value (which is an empty string) otherwise.

You may use string.IsNullOrEmpty() method.

if (!string.IsNullOrEmpty(Request.Form["name"]))
{
  Response.Write("<br/>");
  Response.Write("Name OK");
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
-1

Base on this site: https://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page

This is the answer. Remark this line:

settings.AutoRedirectMode = RedirectMode.Permanent;

in routeConfig.cs

Samuel Surya
  • 433
  • 5
  • 10