在ASP.NET页面中动态添加JavaScript的最佳实践是什么?

6
这是我正在处理的实际代码,它从查询字符串中获取一个ID,检索对象并解析为json。我需要在客户端存储和操作这些数据。最好的方法是将生成的json字符串设置为客户端对象吗?
注意:NewObjectCase是一个具有Get(strID)方法的类,返回一个新对象。Tools.GetQueryObject(string key)是一个返回键的字符串值的方法。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.Script.Serialization;
    using System.Web.UI.WebControls;

        public partial class Testing: System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string strJson = new JavaScriptSerializer().Serialize(NewObjectCase.Get(Tools.GetQueryObject("id")));
                    // add in js to page that will set an client-side js object to this generated json
                }
            }
        }
4个回答

5

有几种简单的方法可以完成这项工作:

1:使用ClientScriptManager.RegisterClientScriptBlock函数调用,直接将脚本插入页面:

  protected void Page_Load(object sender, EventArgs e) {

    // Not sure what your Tools.GetQueryObject is doing, but it should at 
    // the least be performing a UrlDecode to convert the string from any 
    // Url encoding, and as you're about to pass this back to javascript, 
    // you should also HtmlEncode it.
    string valueFromCodeBehind = "var myValue = " +
              Server.HtmlEncode(Server.UrlDecode(Request.QueryString["id"]));

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Output the script block to the page.
    // Notes:
    // 1) I'm passing true as the final parameter to get the script manager
    //    to auto generate the script tags for me.
    // 2) You might need to call "RegisterStartupScript" if you need the  
    //    JS variables earlier in the page.
    cs.RegisterClientScriptBlock(this.GetType(),
                                 "SetValues", 
                                 valueFromCodeBehind,
                                 true);
  }
}

2:在代码后端上的属性,在页面端引用:

在您的.aspx页面中,可以像这样:

<script type="text/javascript">
  var myValue = <%=ValueFromCodeBehind%>
</script>

在代码后台中,您声明变量并确保其已设置:
public partial class Testing: System.Web.UI.Page { 
  protected string ValueFromCodeBehind;

  protected void Page_Load(object sender, EventArgs e) {

    ValueFromCodeBehind = 
              Server.HtmlEncode(Server.UrlDecode(Request.QueryString["id"]));
  }

1

这是我的原始计划,但似乎有更好的方法来添加JSON对象,在智能感知中我注意到Page.RegisterClientScriptBlock()旁边有[已弃用]。

Page.RegisterClientScriptBlock("clientScript",
    string.Format("<script type='text/javascript' language='javascript'>objCase = {0}</script>", strJson));

0

主页面中添加Javascript文件链接上发现了这个。

protected void Page_Load(object sender, EventArgs e)
{
  Page.ClientScript.RegisterClientScriptInclude("somescript", ResolveUrl("some.js"));
}

0

这将在页面顶部注册您的脚本。

ClientScriptManager.RegisterClientScriptBlock Method (Type, Key as String, Script as String)

添加类型和键可以确保只向该类型和键的页面添加一个脚本版本。

在RegisterClientScriptBlock方法的这个重载中,您必须确保在脚本参数中提供的脚本被包装在一个元素块中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接