Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

ASP.NET web page goes blank with JavaScript alert()

, 14 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Here is a simpler version. Using the overload method[^](parameter addScriptTags), you can reduce the code.private void ShowMessage(string Message){ if (!ClientScript.IsClientScriptBlockRegistered("MyMessage")) { ClientScript.RegisterClientScriptBlock(this.GetType(), "MyMessage",...
Here is a simpler version. Using the overload method[^](parameter addScriptTags), you can reduce the code.
private void ShowMessage(string Message)
{
  if (!ClientScript.IsClientScriptBlockRegistered("MyMessage"))
  {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "MyMessage", "alert('" + Message + "');", true);
  }
}
 
public void btnTest_Click(Object sender, EventArgs e)
{
  ShowMessage("Hello World");
}
For AJAX - Updatepanel, use the below one:
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), "MyMessage", Message, true);
 
EDIT
The alert keyword moved into general function ShowMessage.

Further Reading

License

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

About the Author

thatraja
Web Developer
India India
Programmer | Dude | ||||||
 
About him & his blog? check out here. Currently he's updating the site, he'll return soon there with more things*.
 
In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon.
 
Favorite techs are MVC, WCF, Mobile, HTML5, CSS3, jQuery, etc.,
 
His hobbies are watching movies, CP, being alone, etc., But now he's working on something*.
 
* - SCAMP???
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 1 The user will need to pass the hardc... PinmemberBalaji Birajdar14-Oct-11 5:42 
Reason for my vote of 1
The user will need to pass the hardcoded javascript 'alert' keyword everytime this function is called. Overall , the alternative is not generic in nature.
 
The major mistake in this code is , it does not defines the <script>...</script> tags for javascript.
 
If you make this change, then it will be the same as the code above and will not be an 'alternative' as we say
GeneralRe: I agree that I forgot to add the alert keyword in the genera... Pinmvpthatraja14-Oct-11 5:57 
GeneralRe: Reason for my vote of 1The user will need to pass the hardc... Pinmemberjunaidcodeproject22-Aug-13 20:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 14 Oct 2011
Article Copyright 2011 by thatraja
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid