Calling Javascript from C# Code

T

TarheelsFan

I am creating a web site that allows admins to edit the website from
within the site (add contacts, events, etc.) After modifying the
content by either adding, editing, or deleting some info, I want to
display a Javascript alert.

When I write the Javascript in the markup and add the event to the
attributes of the submit button, everything works fine. The only
problem with that is the confirmation alert will run regardless of
whether there is an error or not. So I was going to try to write the
alert in the submit function using: Response.Write("<script
language=javascript>alert('text');</script>"); However, when I do
this, it changes the text formatting on the page. Some text that is
originally white changes to black, and all of the text increases in
size.

Can anyone see what I am doing wrong?

Thanks in advance.
 
A

Alberto Poblacion

TarheelsFan said:
I am creating a web site that allows admins to edit the website from
within the site (add contacts, events, etc.) After modifying the
content by either adding, editing, or deleting some info, I want to
display a Javascript alert.

When I write the Javascript in the markup and add the event to the
attributes of the submit button, everything works fine. The only
problem with that is the confirmation alert will run regardless of
whether there is an error or not. So I was going to try to write the
alert in the submit function using: Response.Write("<script
language=javascript>alert('text');</script>"); However, when I do
this, it changes the text formatting on the page. Some text that is
originally white changes to black, and all of the text increases in
size.

Can anyone see what I am doing wrong?

If you do a Response.Write like that, it will be sent to your output in
whatever position is current at that moment, probably outside of the HTML
element. This will likely confuse the browser, and the results will be
unpredictable. Instead of Response.Write, use one of the methods that the
ClientScriptManager class provides for registering scripts. For instance:

Page.ClientScript.RegisterStartupScript(GetType(), "whatever",
"alert('text');", true);

Note that this is new to asp.net 2.0. If you are using an earlier version,
the procedure is slightly different.
 

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

Top