zoukankan      html  css  js  c++  java
  • ASP.NET后台输出js脚本代码

    利用asp.net输出js我们大多数都会直接使用Respone.Write()然后根js格式的代码,再在页面调用时我们直接这样是完全可以实现的,下面我来给大家介绍另一种方法

    我是我最初的想法以下是代码片段:

    Respone.Write(“hello word!”);

    但是,当你查看客户端源码时,你会发现,输出的内容呈现在源码的最前端,显然它破坏了HTML的格式,在某些情况下这是会影响到页面布局等效果的。正确的输出方式应该是:

    this.ClientScript.RegisterStartupScript
    
    this.ClientScript.RegisterClientScriptBlock.

    this.ClientScript.RegisterStartupScript是在Form开始的第一行注册脚本,后者则是在Form结尾处注册脚本。这样就不会破坏HTML得格式了,如:

    this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "")
    
    this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "alert('hello word!');",True) 

    this.ClientScript.RegisterClientScriptBlock也类似UpdatePanel

    当你想在UpdatePanel内输出一段JS时,运用以上方法就会得不到预期的效果。那么请看一下示例。

    有一个UpdatePanel的ID是upPn

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

    这样的话,当UpdatePanel内容加载到客户端后,就会弹出“hello word!”对话框。

    这样的话,从后台输出JS就更加方便了

    还有一种办法,就是像生成xml一样直接生成js文件了,这样直接调用js文件,就可以了,实例

    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文件调用乱码解决方法

    1、 问题:后台字体倒显示?效果如下:

    原因:由于Asp.net采用UTF-8编码,原先使用GB2312导致乱码。

    解决方法:在Web.config中添加 以下代码段

    <system.web>
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="zh-CN" culture="zh-CN" fileEncoding="utf-8" />
    </system.web>

    在解决完后台乱码问题,接着出现前台乱码问题,详情问题2

    2、 问题:在添加完以上节点后,系统前台页面出现以下乱码问题:

    原因:由于添加了 fileEncoding="utf-8"该项目,造成导航无法显示

    解决方法:删除该选项

    3、 问题:由系统后台生成的JS文件,在前台的*.aspx的页面中调用时乱码,效果如下:

    原因:JS采用的是GB2312编码,而*.aspx采用的UTF-8编码方式,解决思路,统一编码方式

    解决方法:第一步:根据问题1解决方法操作,注:不要加 fileEncoding="utf-8"

    第二步:在需要调用到JS的aspx页中加入 <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />

    第三步:在页面加载事件中加入下句

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    }
  • 相关阅读:
    POI Excel解析
    mysql忘记root密码的解决方法
    注解实现AOP
    Java 集合类
    easyui-textbox 绑定事件
    MarqueeLabel 跑马灯
    Swift-2.14构造过程
    Swift-2.13继承
    Swift- 2.12下标脚本
    Swift-2.11方法
  • 原文地址:https://www.cnblogs.com/zxlovenet/p/3522537.html
Copyright © 2011-2022 走看看