Search code examples
c#asp.netif-statementisnulloremptyispostback

ASP.net C3 IF condition ispostback


Can anyone explain to me what this conditions means, thanks in advance.

if (!String.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack)
{
    code = Request["code"].ToString();
}

Solution

  • The supplied Snippet will check for two conditions 1. !String.IsNullOrEmpty(Request["code"]) and 2. !Page.IsPostBack and Will execute the statement code = Request["code"].ToString(); only if both the conditions ware true.

    More Details about the two conditions mentioned in the if():

    1. String.IsNullOrEmpty(Request["code"]):

    String.IsNullOrEmpty() Indicates whether the specified string is null or an Empty string. It will return true if the value parameter is null or an empty string (""); otherwise, false.

    2. Page.IsPostBack:

    Page.IsPostBack Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback. It will return true if the page is being loaded in response to a client postback; otherwise, false.

    Note : In both the conditions ! symbol will negates the return value from the function. That means if Page.IsPostBack() returns true then !Page.IsPostBack() will convert it to false and wise versa.

    Summary:

    The code will check whether the query-string parameter "code" (Request["code"]) is null or empty, and check whether it is a postback event or not, only when the first condition is true(ie., Request["code"] have some value other than "").