Message Box using Java Script
-
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
-
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
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();");
) thePopWindow()
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 anonclick
attribute to the button, but in theButton1_Click(object sender, System.EventArgs e)
method after validating if you need to show a popup just usePage.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. -
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();");
) thePopWindow()
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 anonclick
attribute to the button, but in theButton1_Click(object sender, System.EventArgs e)
method after validating if you need to show a popup just usePage.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.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
-
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
Why are you dynamically creating the PopWindow function. Surely if it's static you can just include it in the head of the page?
-
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
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
-
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
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.