ClientScript.RegisterStartupScript

G

Guest

Hi,

On a click event of a button I do some validation.

If my validation is unsuccessful I display an alert to the user as below.

string message = "This is a test alert.";
ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script
language=JavaScript>alert('" + (message) + "');</script>");

The alert gets displayed in FireFox but not in Internet Explorer.

Anyone know why?

Thanks.
 
M

Mark Rae

Hi,

On a click event of a button I do some validation.

If my validation is unsuccessful I display an alert to the user as below.

string message = "This is a test alert.";
ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script
language=JavaScript>alert('" + (message) + "');</script>");

The alert gets displayed in FireFox but not in Internet Explorer.

Try this:

string strMessage = "This is a test alert."; // be careful of using
reserved words
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"<script>alert('" + strMesssage + "');</script>");
 
G

Guest

Still does'nt work.

This is what I have
string message = "The record cannot can be Unlocked.";
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"<script>alert('" + message + "');</script>");

I have no idea why it works in FireFox(v1.5.0.5) and not IE6.
 
G

Guest

I will do some more digging. Maybe something with how my Page is composed.

Thanks.
 
G

Guest

I just created a ASP.NET 2.0 Web Project with one page

Below is the entire codebehind. This still does'nt work in IE. I'm baffled.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string message = "This is a test.";
ClientScript.RegisterStartupScript(typeof(Page), "alert",
"<script language=JavaScript>alert('" + message + "');</script>");

}
}
}
 
M

Mark Rae

I will do some more digging. Maybe something with how my Page is composed.

When you do a View Source, what does the JavaScript look like? Is it
possible that it has word-wrapped?

I know that shouldn't be an issue, but I've seen similar effects before...
 
M

MatsL

Worked for me in IE 6...
Did you check so that you have
<script language=JavaScript>alert('This is a test.');</script>
in your source?

Try entering javascript:alert('This is a test.'); in your address field
of the browser.

//Mats
 
M

Mark Rae

Try entering javascript:alert('This is a test.'); in your address field of
the browser.

Yes indeed, and the OP should also check that he has not disabled script
debugging and has enabled display a notification about every script error...
 
L

Laurent Bugnion

Hi,
I just created a ASP.NET 2.0 Web Project with one page

Below is the entire codebehind. This still does'nt work in IE. I'm baffled.

string message = "This is a test.";
ClientScript.RegisterStartupScript(typeof(Page), "alert",
"<script language=JavaScript>alert('" + message + "');</script>");

<snip>

Additionally to what others suggested, the correct syntax for a script
tag is:

<script type="text/javascript">...</script>

The "language" attribute is deprecated. Beside, you should always
enclose attributes values in quote signs. I don't think that it's the
cause for the failure in IE, but who knows.

HTH,
Laurent
 
Joined
Nov 14, 2007
Messages
1
Reaction score
0
No one has answered this correctly yet.

IE6 & IE7 have a different refresh behavior obviously.

Page.ClientScript.RegisterStartupScript( will not fire again when you reload the page for some reason in these browsers.

Unfortunately I have not been able to find a workaround. And yet I can't just use <body onload="blah()"> because this isn't an exclusive master page. I need it to work as it was intended, yet doesn't. :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top