Click to See Complete Forum and Search --> : [RESOLVED] call javascript function from ASP.NET?


toraj58
November 26th, 2008, 08:50 AM
how to call a java script function like alert('hello') in ASP.NET in the event handlers like:
Page_Load or something like Button1_Click.

i messed up with ASP.NET about 1 hours to show a pop up message box just say hello and i forced to write a class (Msg) with about 50 lines of code with a static Method (Show) just to do something that is very easy in PHP and JavaScript.

i wonder if there is an easier way to do it.
i just want show a message to the user (in a pop-up window) when he clicked a button!
please let me know what is the easiest way. thanks.

eclipsed4utoo
November 26th, 2008, 02:26 PM
the easiest way?

in a button click event....

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


that should work.

EDITED: forgot the quotes inside the Write method. Thanks dannystommen.

dannystommen
November 27th, 2008, 03:07 AM
the easiest way?

in a button click event....

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


that should work.

you forgot " "

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

toraj58
November 27th, 2008, 05:36 PM
Response.Write will add the javascript code to the output page(HTML) but in which place of the page? in the begining of the page (before <html> tag) or at the end of the page.

consider i want to use it in the .CS file not inline code in .ASPX.
somwthing like MessageBox.Show in C Sharp Windows Application will be better. any idea?

eclipsed4utoo
November 27th, 2008, 10:11 PM
Javascript is the most common way of showing pop-ups or opening second forms with ASP.Net. You could use AJAX also.

toraj58
November 29th, 2008, 09:10 AM
thanks, but my question is by using Response.Write in the code-behind c# how i can control the place that the java script will be written in the output page.

maybe it would be better that i use single parameter data binding in the HTML matkup to cotrol it:

...
<body>
<?# script_goes_here>
...

is this better?

MMH
December 4th, 2008, 03:50 AM
You can simply add the attributes to the button

Add this in the OnInit event.

Button1.Attributes.Add("onClick", "alert('Hello')");

toraj58
December 6th, 2008, 03:39 AM
yes, thanks; this approach easier.