Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Message Box using Java Script

Message Box using Java Script

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavatoolsquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    psmukil
    wrote on last edited by
    #1

    Hi all, Im using C# as my code behind, im having a code in the button click event where im checking whether a file is exists, if it exists then it show the confirmation message box like "file found, want to continue", but its happening if we register the script in the page load and its not checking the condition. By clciking the button itself its showing the message box. i need that script should get pop up only after the condition get satisfied. Is it possible? Please help me out to solve this problem. Thanks a lot in advance. The code is given below private void Page_Load(object sender, System.EventArgs e) { i = "file Exists"; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); if(!Page.IsClientScriptBlockRegistered("clientscript")) { Page.RegisterStartupScript("clientscript",str.ToString()); } Button1.Attributes.Add("onclick","PopWindow();"); } private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; Button1.Attributes.Add("onclick","PopWindow();"); int a = 2; int z = 2; z = z+ a; Response.Write(z.ToString()); } } Regards Mukilan. REgards Mukilan

    M 1 Reply Last reply
    0
    • P psmukil

      Hi all, Im using C# as my code behind, im having a code in the button click event where im checking whether a file is exists, if it exists then it show the confirmation message box like "file found, want to continue", but its happening if we register the script in the page load and its not checking the condition. By clciking the button itself its showing the message box. i need that script should get pop up only after the condition get satisfied. Is it possible? Please help me out to solve this problem. Thanks a lot in advance. The code is given below private void Page_Load(object sender, System.EventArgs e) { i = "file Exists"; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); if(!Page.IsClientScriptBlockRegistered("clientscript")) { Page.RegisterStartupScript("clientscript",str.ToString()); } Button1.Attributes.Add("onclick","PopWindow();"); } private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; Button1.Attributes.Add("onclick","PopWindow();"); int a = 2; int z = 2; z = z+ a; Response.Write(z.ToString()); } } Regards Mukilan. REgards Mukilan

      M Offline
      M Offline
      Mircea Grelus
      wrote on last edited by
      #2

      Well the problem is that client side code always get executed before the page is sent to the server. So if your button has a server-side event and also a client side one (Button1.Attributes.Add("onclick","PopWindow();"); ) the PopWindow() will get called when you press the button and after that the page will be send to the server. What you need to do is register the client script to be shown after the page has been submitted. So do not add an onclick attribute to the button, but in the Button1_Click(object sender, System.EventArgs e) method after validating if you need to show a popup just use Page.RegisterClientScriptBlock("msg","PopWindow(););. This will cause the PopWindow javascript method to be called after the page loads on the clients side. regards, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.

      P 1 Reply Last reply
      0
      • M Mircea Grelus

        Well the problem is that client side code always get executed before the page is sent to the server. So if your button has a server-side event and also a client side one (Button1.Attributes.Add("onclick","PopWindow();"); ) the PopWindow() will get called when you press the button and after that the page will be send to the server. What you need to do is register the client script to be shown after the page has been submitted. So do not add an onclick attribute to the button, but in the Button1_Click(object sender, System.EventArgs e) method after validating if you need to show a popup just use Page.RegisterClientScriptBlock("msg","PopWindow(););. This will cause the PopWindow javascript method to be called after the page loads on the clients side. regards, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.

        P Offline
        P Offline
        psmukil
        wrote on last edited by
        #3

        thank u very much for your suggestion but if i register code after checking the condition, that message box is not getting pop upped, in the browser if we click button, the "popwindow()" string is getting displayed Code is given below, private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); Page.RegisterClientScriptBlock("clientscript","PopWindow();");// Here is the problem Another Way Page.RegisterClientScriptBlock("clientscript",PopWindow()); // its giving build error as follows(without specified in the quotes) c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(79): The name 'PopWindow' does not exist in the class or namespace 'WebApplication1.WebForm1' int a = 2; int z = 2; z = z+ a; } } Mukil

        P G M 3 Replies Last reply
        0
        • P psmukil

          thank u very much for your suggestion but if i register code after checking the condition, that message box is not getting pop upped, in the browser if we click button, the "popwindow()" string is getting displayed Code is given below, private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); Page.RegisterClientScriptBlock("clientscript","PopWindow();");// Here is the problem Another Way Page.RegisterClientScriptBlock("clientscript",PopWindow()); // its giving build error as follows(without specified in the quotes) c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(79): The name 'PopWindow' does not exist in the class or namespace 'WebApplication1.WebForm1' int a = 2; int z = 2; z = z+ a; } } Mukil

          P Offline
          P Offline
          Paddy Boyd
          wrote on last edited by
          #4

          Why are you dynamically creating the PopWindow function. Surely if it's static you can just include it in the head of the page?

          1 Reply Last reply
          0
          • P psmukil

            thank u very much for your suggestion but if i register code after checking the condition, that message box is not getting pop upped, in the browser if we click button, the "popwindow()" string is getting displayed Code is given below, private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); Page.RegisterClientScriptBlock("clientscript","PopWindow();");// Here is the problem Another Way Page.RegisterClientScriptBlock("clientscript",PopWindow()); // its giving build error as follows(without specified in the quotes) c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(79): The name 'PopWindow' does not exist in the class or namespace 'WebApplication1.WebForm1' int a = 2; int z = 2; z = z+ a; } } Mukil

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            psmukil wrote:

            StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); Page.RegisterClientScriptBlock("clientscript","PopWindow();");// Here is the problem

            You are only putting the code for the function in the StringBuilder, you don't use it for anything. The StringBuilder doesn't automatically recognise that you have created a script, and that there is a web page where the script could go. Neither does the RegisterClientScriptBlock automatically recognise that you happen to have a StringBuilder that contains a script with a function that corresponds with the function call in the code that you do add to the page. --- b { font-weight: normal; } -- modified at 8:28 Tuesday 4th July, 2006

            1 Reply Last reply
            0
            • P psmukil

              thank u very much for your suggestion but if i register code after checking the condition, that message box is not getting pop upped, in the browser if we click button, the "popwindow()" string is getting displayed Code is given below, private void Button1_Click(object sender, System.EventArgs e) { if (i=="file Exists") { int x= 0; int y =0; x = x+ y; StringBuilder str = new StringBuilder(); str.Append(""); str.Append("function PopWindow()"); str.Append("{"); str.Append("alert('hello world')"); str.Append("}"); str.Append(""); Page.RegisterClientScriptBlock("clientscript","PopWindow();");// Here is the problem Another Way Page.RegisterClientScriptBlock("clientscript",PopWindow()); // its giving build error as follows(without specified in the quotes) c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(79): The name 'PopWindow' does not exist in the class or namespace 'WebApplication1.WebForm1' int a = 2; int z = 2; z = z+ a; } } Mukil

              M Offline
              M Offline
              Mircea Grelus
              wrote on last edited by
              #6

              The problem is that you don't have any function called PopWindow() in your rendered page. Why? Because you didn't place it there. You have just defined the StringBuilder in you method, but didn't send it to the page. 1. Put the Javascript in a .js file and inlcude a link to it from HTML or 2. Register the javascript in the page on page_load just as you do the function call. regards, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups