Objective
In the dynamic output JavaScript we are accustomed to using Response.Write ("<script language=\" javascript\ "type=\" text/javascript\ ">alert" (1); </script> ") Such statements to dynamically output, but you may not notice the inside of the format problem, for example, I test the connection database, if the connection failed to print catch information, but you will find that your direct output is not output, will error, such as a string does not end the script error.
Body
First, the general output problem analysis, testing
1. Let's look at a piece of code first:
<summary>
Connecting to a database
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Btnconnect_click (object sender, EventArgs e)
{
Try
{
Fill in the code to connect to the database here
}
catch (Exception ex)
{
Response.Write (Alert (String). Concat (Connection failed!!) Cause of the error: ", ex." message)));
}
}
<summary>
Pop-up information
<script language= "javascript" type= "Text/javascript" >
Alert (msg);
</script>
</summary>
<param name= "MSG" ></param>
<returns></returns>
public static string Alert (String msg)
{
Return Javascript (String. Concat ("Alert ('", MSG, "');");
}
<summary>
Output JavaScript code
<script language= "javascript" type= "Text/javascript" >
Alert ("Pop-up box example!");
</script>
</summary>
<param name= "Context" ></param>
<returns></returns>
public static string Javascript (String context)
{
return string. Concat ("<script language=\" javascript\ "type=\" text/javascript\ ">", Context, "</script>");
}
Description Analysis: Here call method Btnconnect_click let him value failure, such as connection string error, where the connection string is assumed to be: Data source=.\;initial catalog=test; User Id=sa; Password=sa. Then ex when debugging. Message information Displays the database that was requested in failed to open login ' test '. Login failed. \ r \ n user ' sa ' login failed. "So now I can pretty much tell you that your direct output will definitely report a JavaScript error, show" really ", and trace finds that the string it outputs is as follows:
<script language="javascript" type="text/javascript">alert('连接失败!!出错原因:无法打开登录 'test' 中请求的数据库。登录失败。
用户 'sa' 登录失败。');</script>
We copied this code directly into the ASPX page, and it was a mistake, and the error is more obvious, alert output information is not on a line, also does not support C # ' @ ', the problem is here!!