Html Request.Form["name"] 何时为空,何时为空字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8712238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:19:04  来源:igfitidea点击:

When is Request.Form["name"] null and when an empty string?

asp.nethtmlwebforms

提问by Birdman

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

即使文本框是空的并且甚至没有触及回发,为什么以下结果会导致 true if 子句?:

<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? :

为什么以下结果会在第一页加载时产生 true if 子句,但不会在回发时产生?:

<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");
}
%>

回答by Heinzi

First, let me answer your question:

首先,我来回答你的问题:

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

第一页负载是GET,回发是POST(故名回)。当页面通过表单 POST 加载时才会Request.Form填充。

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

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

  • 在第一页加载时,Request.Form是一个空集合。由于Request.Form是 a NameValueCollection访问不存在的条目将返回 null。因此,在第一页加载时Request.Form["whatever"]返回null

  • 回发后,Request.Form充满了值。由于 HTTP POST 不知道null值,Request.Form["whatever"]因此为存在但为空的字段返回空字符串。

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

如果您想避免该x != null && x != ""模式,请使用String.IsNullOrEmpty空合并运算符: (x ?? "") != ""



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

另一方面,您可以通过使用内置的 WebForms 功能而不是Request.Form自己解析来使您的生活更轻松

<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.Textdefaults to "", there's no need to check for nullhere.

由于TextBox.Text默认为"",因此无需在null此处检查。

回答by adatapost

Request.Formis NameValueCollection, which returns nullif specified keyis not found, returns value (which is an empty string) otherwise.

Request.Formis NameValueCollectionnull如果key未找到指定则返回,否则返回值(这是一个空字符串)。

You may use string.IsNullOrEmpty()method.

你可以使用string.IsNullOrEmpty()方法。

if (!string.IsNullOrEmpty(Request.Form["name"]))
{
  Response.Write("<br/>");
  Response.Write("Name OK");
}

回答by Pallavi Virkar

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

Request.Form["ControlName"]null如果表单上不存在控件,则返回。

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

如果 Control 存在,但它包含null或为空值,Request.Form["ControlName"]则将始终返回String.Empty

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

所以这是一个很好的做法,而不是比较(Request.Form["ControlName"] != null),使用(!String.IsNullOrEmpty(Request.Form["ControlName"]))