Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Alert Message / Blank screen 1

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
When I post a JavaScript alert my web browser goes white. Is there a way of getting around this? Have you had this problem?

Response.Write("<script language='javascript'>alert('Hello')</script>")

How do I fix this?

Thanks,
Harold
 
Do you mean that you want the alert box to appear on top of the page that has html already rendered?
 
Code:
StringBuilder sBuilder = new StringBuilder("<script language='javascript'>function window.onload(){");
sBuilder.Append("alert('Hello');");
sBuilder.Append("}</script>");

if(!Page.IsClientScriptBlockRegistered("doAlert"))
{
  Page.RegisterClientScriptBlock("doAlert", sBuilder.ToString());
}
This will display the alert on top of the page after all html elements are rendered.
 
My problem is when I click a button an alert is prompted. It pops up correctly but the background screen turns white. Will your code help this problem?

Thanks,
Harold
 
Umm... ietprofessional - are you wanting the popup to happen on a button click (your problem statement above isn't totally clear). If so...

Code:
protected System.Web.UI.WebControls.Button btn;

public void Page_Load(object sender, EventArgs e)
{
    btn.Attributes.Add("onclick", "alert('hello');");
}

(ps - the code above was hand-written, so if there are any errors, it's not my fault ;))

If you're trying to get the popup happenning on a click event, this method is the right way to go. Doing it the way you've shown above forces the page to post back and then handle the popup instead of handling it right on the client click. (Make sense?)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
If you need the alert to just pop up on the client, w/out any server side logic done prior to it, then AtomicChip's solution will work the best.
 
Atomic, LV -- this was discussed a week or so ago; isn't it true that you must use an HTML button, and not an ASP.NET button (which requires postback) to correctly carry out a java alert? Seems like the last time I tested this (add.attribute) with an <asp:Button... it didn't work, and I used an HTML button instead, q.v.,

Code:
<INPUT TYPE="button" VALUE="Y1" onClick="javascript:
alert('hello');" style="font-size: 14px; font-weight: bold; color: #0000FF;" width="28px"> </b></Font>
 
isadore,

true, this could work fine if you do not require a postback. the addattribute method works fine. i use it all over the place in my applications.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Im just guessing :) if your code is more complex and your javascript too could it be that you have to put a "return true" in your javascript code?

--------------------------------------
deleau@gmail.com
 
LV, can you write the following code so that it can be used in a class? I want to be able to use this code though out my web applications.

Thanks,
Harold

StringBuilder sBuilder = new StringBuilder("<script language='javascript'>function window.onload(){");
sBuilder.Append("alert('Hello');");
sBuilder.Append("}</script>");

if(!Page.IsClientScriptBlockRegistered("doAlert"))
{
Page.RegisterClientScriptBlock("doAlert", sBuilder.ToString());
}
 
You can create a class that inherits from Page class and have a method with the above functionality in it. Then your web page should inherit from this class (instead of default System.Web.UI.Page) and the method will become available for a call. In the sample below the class is just added to the project namespace. Kind of like this (please test it, 'cause I did not):

code in class:
Code:
using System;
using System.Text;

namespace MyProject
{	
  public class MyClass : System.Web.UI.Page  
  {
    // default constructor
    public MyClass()
    {
			
    }

    protected void doAlert(string msg)
    {
      StringBuilder sBuilder = new StringBuilder("<script language='javascript'>function window.onload(){");
      sBuilder.Append("alert('" + msg + "');");
      sBuilder.Append("}</script>");

      if(!this.IsClientScriptBlockRegistered("doAlert"))
      {			
         this.RegisterClientScriptBlock("doAlert", sBuilder.ToString());
      }	
    }
  }
}

now code on the page:
Code:
//public class MyWebPage : System.Web.UI.Page
public class MyWebPage : MyClass
{
  private void Button1_Click(object sender, System.EventArgs e)
  {
    doAlert("just testing");		
  }
}
I think you got the idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top