javascript alert in C# web form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waterfall
    New Member
    • Dec 2007
    • 22

    javascript alert in C# web form

    how to use javascript alert in C# web forms?
    im using this:
    Code:
     
    Button.attributes.Add("onClick","javascript:alert('data saved successfully')");

    but its not working.
    i searched for it on internet but im getting tutorials etc for windows forms.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Do you already have a button click assigned for this action? Like in the C# code?
    If you do, it will probably be run FIRST and since the page reloads on button clicks like that(postback) it no longer has any knowledge of your button press.

    Comment

    • enggwaqas
      New Member
      • Jun 2007
      • 19

      #3
      Originally posted by waterfall
      how to use javascript alert in C# web forms?
      im using this:
      Code:
       
      Button.attributes.Add("onClick","javascript:alert('data saved successfully')");

      but its not working.
      i searched for it on internet but im getting tutorials etc for windows forms.
      If you are using .net 2.0 you can use the OnClientClick property to show javascript alert. In this case javascript function will always call first.

      Code:
      <asp:Button ID="btnShow" runat="server" OnClientClick="javascript:alert('Message to display');" />

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by waterfall
        how to use javascript alert in C# web forms?
        im using this:
        Code:
         
        Button.attributes.Add("onClick","javascript:alert('data saved successfully')");

        but its not working.
        i searched for it on internet but im getting tutorials etc for windows forms.

        Try:
        [code=cpp]
        Button.attribut es.Add("onclick ","JavaScript:a lert('data saved successfully'); return false;");
        [/code]

        The "return false;" will prevent a postback.

        -Frinny

        Comment

        • balame2004
          New Member
          • Mar 2008
          • 142

          #5
          Try this:

          Button.attribut es.Add("onclick ","<script>Aler t('data saved successfully'); </script>");

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            Can u tell us wht is problem and on which event u add attribute or u can try onClientClick event of button in HTML view
            Last edited by Plater; Mar 14 '08, 01:00 PM. Reason: typo

            Comment

            • mnemukula
              New Member
              • Aug 2008
              • 2

              #7
              there are cases when u need to customize your message server side. if you are using .Net 2.0 you can put a code like the one below on your PageLoad event.

              String script = "javascript:ale rt('{0}');";
              btnTest.Attribu tes.Add("onclic k", String.Format(s cript, btnTest.Text));

              this code snippet assumes that u have a button named btnTest. the reason i used onclick as oppesed to onClient click is because the browser render it as onclick as run time.

              Hope this works

              Comment

              • shahalashamo
                New Member
                • Aug 2008
                • 3

                #8
                I test it,It works very vell,maybe you have other errors in other codes.

                Comment

                Working...