Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I tried to display pop up alert from c# code behind file.
My problem is that when the pop up statement is the only thing to be executed, it goes very well. But when it is combined with some other statement, it does not work.

For example,
this works
C#
if(condition)
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true);
            }


but this don't

C#
if (condition)
               {
                   emp.logout();
                   System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true);
               }



I have tried various other ways but it never work with other code lines.
Anything I am missing?

What I have tried:

if (condition)
{
emp.logout();
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true);
}
Posted
Updated 5-Oct-16 21:53pm
Comments
Karthik_Mahalingam 4-Oct-16 10:37am    
what you doing inside emp.logout();
planetz 5-Oct-16 1:01am    
emp.logout redirects to another page. I have to show pop up before calling the method.
Karthik_Mahalingam 5-Oct-16 1:12am    
ok, what you can do is, show the alert first and then redirect to other page.
planetz 5-Oct-16 1:19am    
have tried that also..but as F-ES Sitecore explained, since the html version will be prepared at the end and before that there is redirection, so pop up part will be discarded.
Karthik_Mahalingam 5-Oct-16 1:23am    
i have posted a solution, give a try on it,
provided you are not doing any logic inside emp.logout();

You have to understand the asp.net page lifecycle and the order things occur in. First your code-behind runs and that generates html (all RegisterStartupScript does is inject javascript into that html), that html is sent to the browser to execute, and it is at that point you see the popup.

If emp.logout redirects the user to a different page then to do that redirect all of the html generated by your code behind is simply discarded and replaced with a page that tells the browser to redirect, so the js that makes your alert is also discarded and is never executed.

The important thing to note is that your .net code isn't running inside the browser, you can't send anything to the browser before the page is finished, and the RegisterStartupScript call doesn't actually execute javascript.
 
Share this answer
 
Comments
Philippe Mori 4-Oct-16 9:32am    
Very well explained.
planetz 5-Oct-16 0:45am    
Yes, the logout method redirects to another page. I understand..!! Thank you so much!!
What if I display alert before logout method? It is not showing then also.
planetz 5-Oct-16 0:55am    
Ok...now I got it..!!
If you want to show the alert before redirection you shall do it in javascript
C#
emp.logout();
               string redirectScript = " window.location.href = 'otherPage.aspx';";
               System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');" + redirectScript, true);
 
Share this answer
 
Comments
planetz 5-Oct-16 1:29am    
I am not redirecting to aspx directly. The logout method contains some updates and then redirection. So I need to display alert and then move on to the method which includes the redirection.
Karthik_Mahalingam 5-Oct-16 1:56am    
oh, then i will have to delete this solution.
planetz 5-Oct-16 1:56am    
I have found a work around. To execute the logic separately and then redirecting separately. Since it is part of the answer, I am not marking your answer as solution.
Karthik_Mahalingam 5-Oct-16 1:56am    
thats fine.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('show my alert message')", true);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900