How to use asp.net output js

Source: Internet
Author: User

The use of asp.net output JS most of us will directly use Respone.write () and then the root of the JS format code, and then the page call when we directly this is completely achievable, I would like to introduce another method

I was my initial idea the following is a code fragment:

Respone.write ("Hello word!");

However, when you look at the client source code, you will find that the output of the content presented at the forefront of the source code, it is obvious that it destroys the HTML format, in some cases this will affect the layout of the page and other effects. The correct way to output should be:

This. Clientscript.registerstartupscript

This. Clientscript.registerclientscriptblock.

This. Clientscript.registerstartupscript is the first line to register the script at the beginning of the form, and the latter to register the script at the end of the form. This will not destroy the HTML format, such as:

This. Clientscript.registerstartupscript (this. GetType (), "Scriptkey", "" "

This. Clientscript.registerstartupscript (this. GetType (), "Scriptkey", "alert (' Hello word! ');", True)

This. Clientscript.registerclientscriptblock is similar to UpdatePanel

When you want to output a section of JS in the UpdatePanel, use the above method will not get the desired effect. So take a look at the example.

There is a UpdatePanel ID is uppn

Scriptmanager.registerclientscriptblock (uppn,this. GetType (), "Scriptkey", "alert (' Hello word! ');", True)

Scriptmanager.registerstartupscript (uppn,this. GetType (), "Scriptkey", "alert (' Hello word! ');", True)

In this case, when the UpdatePanel content is loaded onto the client, "Hello word!" pops up. dialog box.

In this case, the output from the background JS is more convenient

There is another way, just like the generation of XML directly generated JS file, so directly call the JS file, you can, the example

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 ();
}

JS file calls garbled resolution method

1, the problem: Background font inverted display? The effect is as follows:

Reason: Because ASP.net uses UTF-8 code, original use GB2312 lead to garbled.

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

<system.web>
<globalization requestencoding= "Utf-8" responseencoding= "Utf-8" uiculture= "ZH-CN" culture= "ZH-CN" Utf-8 "/>
</system.web>

After solving the background garbled problem, then appear the front desk garbled problem, details 2

2, the problem: After adding the above node, the System front page appears the following garbled problem:

Reason: The navigation cannot be displayed due to the addition of the fileencoding= "Utf-8" item

Workaround: Delete this option

3, the problem: the system generated by the background JS file, in the foreground of the *.aspx page when the call garbled, the effect is as follows:

Reason: JS adopts GB2312 encoding, and *.aspx adopts UTF-8 encoding method, solution, Unified coding method

WORKAROUND: First step: According to the problem 1 solution operation, Note: Do not add fileencoding= "Utf-8"

Step Two: Add <meta http-equiv= "Content-type" content= text/html in ASPX pages that need to be called to JS; charset=gb2312 "/>

Step three: Add the next sentence to the page load event

protected void Page_Load (object sender, EventArgs e)
{
response.contentencoding = System.Text.Encoding.GetEncoding ("GB2312");
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.