json 在www.example.com页面中动态添加javascript的最佳实践是什么asp.net?

ego6inou  于 2022-12-15  发布在  Java
关注(0)|答案(4)|浏览(118)

这是我正在编写的实际代码,它从查询字符串中获取ID,检索对象并解析为json。我需要在客户端存储和操作此数据。将生成的json字符串设置为客户端对象的最佳方法是什么?
注意:NewObjectCase是一个类,它具有返回新对象的方法Get(strID)。Tools.GetQueryObject(字符串键)是一个返回键的字符串值的方法

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
                }
            }
        }
mwngjboj

mwngjboj1#

完成工作的几个简单方法:
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"]));
  }
epggiuax

epggiuax2#

这是我最初的计划,但似乎有更好的方法来添加一个json对象,在智能方面,我注意到它在Page.RegisterClientScriptBlock()旁边[已弃用]。

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

vnzz0bqm3#

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

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

添加类型和键可确保只将该类型和键的一个脚本版本添加到页中。
在RegisterClientScriptBlock方法的此重载中,必须确保script参数中提供的脚本 Package 在元素块中。

i2loujxw

i2loujxw4#

Adding Javascript file link in Master Page上找到此内容

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

相关问题