Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Response.Write("<script language=javascript> alert('"gk_szEnter_Value_Error_Message"'); </script>");

its not displaying the required error msg..its throwing error..pls help
Posted

That is because your code and strings are not properly closed (the double quotes) in your code and the format and syntax of the code is broken there. Change your code to be like this one,

C#
Response.Write(@"<script> 
                      alert('" + gk_szEnter_Value_Error_Message + "'); 
                 </script>");


Now the code would execute, and would stream down the script. The gk_szEnter_Value_Error_Message will now be considered as a variable in your code and will be given the value it has at that moment.

A good way of writing strings is to use the String.Format()[^] function, to create the strings in an efficient way, for avoiding the concatenation; which causes a lot of trouble like it did for you in this scenario.

C#
Response.Write(String.Format("<script>alert('{0}');</script>"),    
                              gk_szEnter_Value_Error_Message);


This would now be an efficient way of avoiding the concatenation problems.

A personal tip, do not specify the language of your script. Browsers know it is JavaScript; unless you're using some other scripting language. I never do write it, and Browser doesn't complain at all. :)
 
Share this answer
 
v2
Comments
Member 11483453 26-Feb-15 18:18pm    
Thanks much...Its working fine :)
Sergey Alexandrovich Kryukov 26-Feb-15 18:37pm    
5ed.
—SA
Afzaal Ahmad Zeeshan 26-Feb-15 18:38pm    
Thanks, Sergey sir.
Santosh K. Tripathi 26-Feb-15 23:34pm    
5+
If does work, even if it is not a valid HTML. :-)
But of course, it shows "gk_szEnter_Value_Error_Message" (which double quotation marks), not the value of the variable. If you expected that, it would be quite weird.

Maybe what you miss is this:
http://support.microsoft.com/kb/307860[^],
https://msdn.microsoft.com/en-us/library/bda9bbfx%28v=vs.100%29.aspx[^],
http://weblogs.asp.net/ahmedmoosa/embedded-code-and-inline-server-tags[^].

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 26-Feb-15 18:13pm    
The behaviour you're talking about comes if the strings are prepended with a @ sign to ignore the " occurance. The OP encountered the error just because he has not closed the strings. At this moment, OP has not yet came all the way down to the browser to ignore the HTML markup problems and let browser do all of its magic. At this moment, any syntax error would cause a yellow-screen of death for him; a parser error.

OP needs to concatenate the strings in the Response.Write() methods, so that the variable data must be written inside the alert box. See my solution 2.
Sergey Alexandrovich Kryukov 26-Feb-15 18:37pm    
I did not notice the unbalanced part, just up-voted your solution.
—SA
Member 11483453 26-Feb-15 18:25pm    
Thanks for the solution:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900