javascript alert() after post back c#

P

PH

Does anyone know how to have the pop-up javascript alert,

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

, pop-up after the page has been posted back? I am pressing a button,
validating data and then wanting to simply pop-up an alert if say the
login is empty. When I click the button though the page is blank but
the pop-up is there.
 
M

Munsifali Rashid

In the postback function, you can use:

[C#]
string strScript = "<script language='JavaScript'>alert('Hey!')</script>";
Page.RegisterStartupScript("PopUp", strScript);

[VB.NET]
Dim strScript As String = "<script
language='JavaScript'>alert('Hey')</script>"
Page.RegisterStartupScript("PopUp", strScript)

However, it might be easier to use the RequiredFieldValidator control to
avoid an unecessary round-trip. If you want to have a popup, you could have
your custom javascript function validate the login field, and pop-up an
alert if it is empty, before submitting the form.

Regards,

Mun
 
D

Dmitriy Lapshin [C# / .NET MVP]

Forms have a client-side event called "onSubmit". You can specify a
JavaScript function that will handle this event and perform client-side
validation before the form is posted back to the server.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think that the best way of doing this is using client side validation,
this avoid the round trip to the server just to report trivials errors as an
empty login.

Take a look at MSDN for how to use client side validators, it;s very easy.

Cheers,
 
T

Todd_M

For the simple validation you describe below, client side validators and
simple javascript may be sufficient. However, in our case, when we needed to
communicate back from the server to the client via a popup "alert", we wrote
a javascript function to call a standard html page that would become the
popup (so it could inherit the stylesheets and not just be a basic alert
box) and then on the main page, we placed a hidden text control.

The text control was unpopulated the first load. On submit, if no message
was needed, the textbox was still left empty. But if we needed to
communicate back, we populated the textbox with a message string from the
server that the onload() could see a) textbox isn't empty, so I need to
initiate the message popup and b) parse the hidden textbox to pull out the
values. The Javascsript function then opened the window, passing the
appropriate values it got from the textbox in the querystring to the message
page. Voila, customized, stylesheet driven alert boxes.

You can take it a step further and allow the popup to communicate back to
the parent then (for example, you might want an OK and CANCEL) so that the
main page can make some response to the user decision. We standardized
that HTML/Javascript function to give most of the standard responses of a
regular message box (OK only, OK/CANCEL, YES/NO, etc.)

It's a pain in the you know what to get it built and hooked up the first
time, but then it's the same thing over and over again when ever you need
it. Put the javascript in a #include and you won't have to be rewriting that
on each aspx/html page.

Todd
 

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