Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the code-behind code
C#
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage",
    "alert("Test Message");", true);

But the alert message does not display.
But if coded like that, this message displays on the top of the page
C#
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('Notification : Your Message.');", true);

Wish you can hint me why. For the 2nd one I revised and tried, but it does not work
C#
string s = "Notification : Your Message.";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert(" + s + ");", true);

Could you provide your advisory? Thanks.

What I have tried:

JavaScript alert not displayed
Posted
Updated 13-Jul-16 7:53am
v2

The difference is the places where it gets rendered. Otherwise, there is no difference. Refer - RegisterStartupScript vs RegisterClientScriptBlock in asp.net - dotnetprof[^].

You have not used the second one correctly. Try like below.
C#
ClientScript.RegisterClientScriptBlock(this.GetType(), "sample", "alert('Test Message')", true);
 
Share this answer
 
Comments
s yu 13-Jul-16 13:53pm    
TS: Thanks for your response. I revised my Q, and added 3nd one. Could you provide your hint for it? My 2nd one is essentially the same as yours and it works fine. Thanks.
So, now it is working right?
Karthik_Mahalingam 13-Jul-16 14:19pm    
obviously your code works.
my 5! for you, since both our solution are same. and mine only marked as answer.
Hehe. Thanks man. :)
try
C#
// this 
 ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage",  "alert('Test Message');", true);
// or  (when using update panel)
 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('my message');", true);


use single quotes for alert message string


C#
string s = "Notification : Your Message.";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + s + "');", true);
 
Share this answer
 
v3
Comments
s yu 13-Jul-16 13:59pm    
Yours works fine. However, how can a variable is included? I tried:
string s = "xxxxxxxxxxxx");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert(" + s + ");", true);
then it does not work.
Karthik_Mahalingam 13-Jul-16 14:00pm    
add single quotes to the variable or in the alert
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('" + s + "');", true);
s yu 13-Jul-16 14:03pm    
Actually I also tried to have a single quote for the string like yours. But it does not work.
Karthik_Mahalingam 13-Jul-16 14:04pm    
show your code
s yu 13-Jul-16 14:08pm    
string s = "xxxxxx";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + s + "');", true);

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