Output js script code in asp. NET background

Source: Internet
Author: User

Using asp.net to output js, most of us will directly use Respone. write () and then the code in the root js format. We can directly implement this method when calling the page. Next I will introduce you to another method.

I initially thought about the following code snippets:

The Code is as follows: Copy code

Respone. Write ("hello word !"); Or output JS Respone. Write ("");

However, when you view the client source code, you will find that the output content is displayed at the front end of the source code. Obviously, it destroys the HTML format, in some cases, this will affect the page layout and other effects.

The correct output method should be:

The Code is as follows: Copy code

This. ClientScript. RegisterStartupScript or this. ClientScript. RegisterClientScriptBlock.

This. ClientScript. RegisterStartupScript

The script is registered at the first line starting with Form, and the latter is registered at the end of Form. In this way, the HTML format will not be damaged, for example:

 

The following is a code snippet:

The Code is as follows: Copy code

This. clientScript. registerStartupScript (this. getType (), "scriptKey", "") or this. clientScript. registerStartupScript (this. getType (), "scriptKey", "alert ('Hello word! '); ", True) this. ClientScript. RegisterClientScriptBlock is similar. UpdatePanel

 

When you want to output a piece of JS in UpdatePanel, the above method will not achieve the expected results. Take a look at the example.

There is an UpdatePanel ID that is upPn

The following is a code snippet:

The Code is as follows: Copy code

ScriptManager. RegisterClientScriptBlock (upPn, this. GetType (), "scriptKey", "alert ('Hello word! '); ", True)

Or

ScriptManager. RegisterStartupScript (upPn, this. GetType (), "scriptKey", "alert ('Hello word! '); ", True)

In this case, when the UpdatePanel content is loaded to the client, "hello word!" will pop up !" Dialog box.

In this way, it is more convenient to output JS from the background.

Another way is to directly generate a js file like an xml file, so that you can directly call the js file.

The Code is as follows: Copy code

Protected override void Render (HtmlTextWriter writer)
{

 

Int titleid = 0;
StringWriter html = new StringWriter ();
System. Web. UI. HtmlTextWriter tw = new System. Web. UI. HtmlTextWriter (html );
Base. Render (tw );
StreamWriter sw;

 

String dir = Server. MapPath ("~ /Js/ask /");

If (! Directory. Exists (dir ))
{
Directory. CreateDirectory (dir );
}

 

String path = dir + "ask" + "_" + titleid + ". js ";

 

Sw = new StreamWriter (path, false, System. Text. Encoding. UTF8 );

 

String newhtml = html. ToString (). Replace ("," "). Replace (" rn ","");
String lasthtml = "document. write (" "+ newhtml + "")";

 

Sw. Write (lasthtml. ToString ());
Sw. Close ();
Tw. Close ();

}

Solution to JS file calling garbled code

1. Question: Is the background font displayed in reverse mode? The effect is as follows:

Cause: Because Asp.net uses UTF-8 encoding, the original use of GB2312 causes garbled characters.

Solution: Add the following code snippet to Web. config:

The Code is as follows: Copy code

<System. web>

<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8" uiCulture = "zh-CN" culture = "zh-CN" fileEncoding = "UTF-8"/>

</System. web>

After the backend Garbled text problem is solved, the front-end Garbled text problem occurs. Problem 2

2. Problem: after adding the above nodes, the system front-end page displays the following garbled problem:


Cause: the project fileEncoding = "UTF-8" is added, leading to Navigation failure.

Solution: delete this option.

3. problem: the JS file generated by the system background is garbled during calling on the *. aspx page at the foreground. The effect is as follows:

Cause: JS uses GB2312 encoding, and *. aspx adopts the UTF-8 encoding method, solve the problem, unified encoding method

Solution: Step 1: Perform troubleshooting Based on Problem 1. Note: Do not add fileEncoding = "UTF-8"

Step 2: Add <meta http-equiv = "Content-Type" content = "text/html; charset = GB2312"/>

Step 3: Add the following sentence to the page loading event

 

The Code is as follows: Copy code
Protected void Page_Load (object sender, EventArgs e)
{
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
}

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.