-4

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

if (!String.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack)
{
    code = Request["code"].ToString();
}
Michael
  • 2,631
  • 2
  • 24
  • 40
John Vega
  • 15
  • 1
  • 7
  • 2
    the code is checking whether a querystring parameter "code" is passed to the page during the first loading of the page. Page.IsPostBack is used to check whether the page is loaded for first time or a call back from the client side ajax call. Request["code"] is the querystring parameter like http://mypage.com/test?code=abc, to check whether a parameter of such is being passed to the page. hope this helps – Roy Feb 23 '16 at 02:29
  • @JohnVega, do you want us to teach you basic .net / c# coding..? if you are not familiar with `string.IsNullOrEmpty` function do a google search also do the same for `IsPostBack` – MethodMan Feb 23 '16 at 02:39
  • im just a bit confused with the exclamation symbol, is !Page.IsPostBack the same as Page.IsPostBack? – John Vega Feb 23 '16 at 03:02
  • the exclamation denotes "not", if (not)Page.IsPostBack – Roy Feb 23 '16 at 03:55

2 Answers2

0

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 "").

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • is !Page.IsPostBack the same as Page.IsPostBack without the exclamation symbol? – John Vega Feb 23 '16 at 03:00
  • No, Both are different. if `Page.IsPostBack()` returns `true` then `!Page.IsPostBack()` will convert it to `false` and wise versa. see updates in the answer – sujith karivelil Feb 23 '16 at 03:34
  • Then that means: IF !String.IsNullOrEmpty(Request["code"]) - contains a value its TRUE IF !Page.IsPostBack - is a post back - is FALSE ???? – John Vega Feb 23 '16 at 03:59
0

if (ispostback is not true)means the page dose not load from the server control and (request["Code"] is not null) means it exist and (request["Code"] is not empty) means it's not equal ""

pst
  • 59
  • 1
  • 2
  • 10