In an exam I have to find out anwser of this question about asp.net.
the question is, if we have a variable named 'pagehits', and we increment this var by 1 in both page_init and page_load functions. After making 20 requests from 20 diffrent clients, what will be the final value of 'pagehits'? I know that page_init fires only once, but if 20 diffrent users send the request, how many times would that fires? So we know that in this senario Page_Load function fires 20 times; does page_init fire only one time or it fires for all 20 clients?
and is the answer pagehits=21 correct or 40?
'pagehits' default value is zero.
thank you very much
CodePudding user response:
does page_init fire only one time or it fires for all 20 clients?
It fires for all 20 clients. The only difference between Init
and Load
is when it fires: Init
comes first, Load
happens later. For details, see the following question:
Note that, if the same client makes another request, both Page_Init
and Page_Load
fire again, so this technique is not suitable for counting "unique visitors".
and is the answer pagehits=21 correct or 40?
If pagehits
is static, it will be 40. If pagehits
isn't, each request has its own instance of the variable, rendering the question moot. Do note, though, that the value of static variables will be lost when the application pool recycles. If that value is important to you, you need to persist it somewhere, for example, in a database or a file.