User M
User M

Reputation: 329

Later 'if' condition getting called before the 'alert' message in c#

Pardon me for the vague description

C# Code:

if(listCount <= minCountRequired)
{
    DisplayError(errMsgForLessCount); // Calls scriptmanager to display alert.
}

if(!ConfirmFromUser)  // asks for User confirmation to continue... getting called first  
{
     //perform some actions... not imp...
}

Method ConfirmFromUser

private bool ConfirmFromUser
{
    string confirmValue = Request.Form["confirm_value"];
    if(confirmValue == "Yes")
          return true;
    else
          return false;
}

Issue: The 2nd 'if' condition is getting called first while execution of the web page. By that I don't mean the contents inside the 2nd if, just the condition... Statements inside first if condition is getting executed later than the 2nd IF condition.

Problem ? Is the problem that the first uses Javascript to display alert and that we are calling that using the ScriptManager and the later is Request.Form? Kind of consistency issue???

Upvotes: 0

Views: 337

Answers (1)

Jacob Krall
Jacob Krall

Reputation: 28825

Your C# code is executing in order. The ASP.NET server is generating the JavaScript and HTML that will be executed in a user's web browser. It will generate all of the output and send it to the client. Then the client can do whatever it wants with that data.

So, if your C# code generates some output that looks like this:

<html><body><script>alert('hi')</script><b>Hello!

The entire contents of this document will be sent to the client. The client will receive the entire document, and begin to execute it. This isn't Pee-Wee's Playhouse—neither C# nor the web browser will stop the data transfer just because the magic word alert() appeared in the output.

Here's a sequence diagram of what is going on. Note that there is no part in the middle where the client and server continue to communicate -- in HTTP, once the request is over, that's it! You have to make another request (e.g. using AJAX, forms, links, etc.) to get more data to the server.

Sequence diagram of the HTTP lifecycle

Upvotes: 2

Related Questions