Asp. NET dynamic output JavaScript text format wrapping problem

Source: Internet
Author: User

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!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.