NEWBIE: passing error to javascript alert

G

gijeet

Hello,
is there a way call a javascript function directly from within asp
page?
Example, here is page load event in C#

protected void Page_Load(object sender, EventArgs e){

// create error
int Var0 = 0;
try {
int iVar1 = 8/Var0;
}

catch (Exception ex){

string sExMsg = ex.Message;


//javascript works fine with string constant
Response.Write("<script type='text/javascript'>alert('In Catch');</
script>");

//can write error to page no problem
Response.Write(sExMsg);

// would like a popup with error
Response.Write("<script language='javascript'>doAlert(" + sExMsg +
");</script>");
Response.Write("<script language='javascript'>function doAlert(sExMsg)
{alert(sExMsg);}</script>");

//write error to log since I can't pop-it-up
//StreamWriter sw = File.AppendText(Server.MapPath("~/error.log"));
//sw.WriteLine(sExMsg);
//sw.Close();

}

}

Any help for a newbie is appreciated

Gi
 
G

gijeet

ClientScript.RegisterStartupScript(GetType(), "error", "alert('In Catch');",

Thanks but not sure how to use this. Where do I put this? class
level before page load event? within page load? do I need a
RegisterStartupScript for each alert I wish to use?

Gi
 
M

Mark Rae [MVP]

Thanks but not sure how to use this. Where do I put this?

Wherever you like, so long as you don't Redirect from the page...

Basically, it will send down the JavaScript to run after the page had
loaded...
do I need a RegisterStartupScript for each alert I wish to use?

Yes.

Incidentally, why are you actually doing this...?
 
G

gijeet

Incidentally, why are you actually doing this...?
basically for debugging. I don't like response.writes and I'm trying
to learn javascript. but if you know of a better way to popup alerts,
let me know.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top