Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:44+00:00 2026-05-27T21:59:44+00:00

Why do the following result in a true if clause even though the textbox

  • 0

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");
}
%>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:59:45+00:00Added an answer on May 27, 2026 at 9:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I execute the following statement: string.Compare(mun, mün, true, CultureInfo.InvariantCulture) The result is '-1',
I am trying to reach the following result here I know how to configure
I would like to iterate. @some_value outputs the following result: { Meta => {
Will the following code result in a deadlock using C# on .NET? class MyClass
I was making the following call: result = RegOpenKeyEx(key, s, 0, KEY_READ, &key); (C++,
On Oracle 9i, why does the following produce the result 'abc' select 'abc '
The following two queries each give the same result: SELECT column FROM table GROUP
What would result from something like the following: p(X,Y) :- q(X). p(X,Y) :- r(Y).
I have the following function: - (NSString *)urlEncodedValue { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(
currently i obtain the below result from the following C# line of code when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.