Tags: alert, back, javascript, languagejavascriptgtalert, ltscript, net, pop-up, response, scriptgt, sharp, write

javascript alert() after post back c#

On .Net » .Net C# (C sharp)

5,803 words with 4 Comments; publish: Mon, 31 Dec 2007 12:56:00 GMT; (100234.38, « »)

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.

All Comments

Leave a comment...

  • 4 Comments
    • 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

      "PH" <p_h082002.net-csharp.questionfor.info.yahoo.com> wrote in message

      news:c51095b4.0312160133.31246c68.net-csharp.questionfor.info.posting.google.c om...

      > 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.

      #1; Mon, 31 Dec 2007 12:57:00 GMT
    • 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.

      --

      Dmitriy Lapshin [C# / .NET MVP]

      X-Unity Test Studio

      http://x-unity.miik.com.ua/teststudio.aspx

      Bring the power of unit testing to VS .NET IDE

      "PH" <p_h082002.net-csharp.questionfor.info.yahoo.com> wrote in message

      news:c51095b4.0312160133.31246c68.net-csharp.questionfor.info.posting.google.c om...

      > 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.

      #2; Mon, 31 Dec 2007 12:58:00 GMT
    • 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,

      --

      Ignacio Machin,

      ignacio.machin AT dot.state.fl.us

      Florida Department Of Transportation

      "PH" <p_h082002.net-csharp.questionfor.info.yahoo.com> wrote in message

      news:c51095b4.0312160133.31246c68.net-csharp.questionfor.info.posting.google.c om...

      > 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.

      #3; Mon, 31 Dec 2007 12:59:00 GMT
    • 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

      "PH" <p_h082002.net-csharp.questionfor.info.yahoo.com> wrote in message

      news:c51095b4.0312160133.31246c68.net-csharp.questionfor.info.posting.google.c om...

      > 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.

      #4; Mon, 31 Dec 2007 13:01:00 GMT