Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

NEWBIE: passing error to javascript alert

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gijeet@yahoo.com - 22 Oct 2007 19:03 GMT
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
Mark Rae [MVP] - 22 Oct 2007 19:13 GMT
> Any help for a newbie is appreciated

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

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gijeet@yahoo.com - 22 Oct 2007 19:58 GMT
> ClientScript.RegisterStartupScript(GetType(), "error", "alert('In Catch');",
> true);

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
Mark Rae [MVP] - 22 Oct 2007 20:36 GMT
>> ClientScript.RegisterStartupScript(GetType(), "error", "alert('In
>> Catch');",
>> true);
>
> 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...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gijeet@yahoo.com - 22 Oct 2007 21:27 GMT
> 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.
Mark Rae [MVP] - 22 Oct 2007 22:35 GMT
>> 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.

There's no need to popup anything in order to debug in Visual Studio.NET -
it's all built-in:
http://www.wrox.com/WileyCDA/Section/id-305946.html

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

PokerJoker - 22 Oct 2007 22:39 GMT
use the debugger to debug.  alert debugging is asp classic style :)

> > 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.
Eliyahu Goldin - 23 Oct 2007 13:38 GMT
This can help you:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage.aspx?page=Articles/HowTo/HowToPassMessagesBet
weenServerAndClient.htm


Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> Hello,
> is there a way call a javascript function directly from within asp
[quoted text clipped - 38 lines]
>
> Gi
Mark Rae [MVP] - 23 Oct 2007 13:50 GMT
> This can help you:
> How to Pass Messages and Actions between Server and Client
> http://usableasp.net/DeveloperPage.aspx?page=Articles/HowTo/HowToPassMessagesBet
weenServerAndClient.htm

Not much use in this particular case, though, as the OP's requirements for
this were for debugging a la ASP Classic...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.