脚本未在页面加载时插入

时间:2021-11-07 07:52:12

I am working on a mock class where I need to fake getting data out of a database and sending it within a json array to javascript. However, I can't get it to work, I've followed several tutorials and examples and it looks like I am missing something.

我正在开发一个模拟类,我需要假装从数据库中获取数据并将其在json数组中发送到javascript。但是,我无法让它工作,我已经遵循了几个教程和示例,看起来我错过了一些东西。

This is my code in the CodeBehind file:

这是我在CodeBehind文件中的代码:

public class Segment : System.Web.UI.Page
{
    public int[,] GetAllSegments()
    {
        int[,] segments = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

        return segments;
    }

    public void Page_Load(object sender, EventArgs e)
    {
        int[,] allSegments = GetAllSegments();

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(allSegments);
        string script = String.Format("<script type=\"text/javascript\">var allSegments={0}</script>", json);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script, true);
    }
}

The script is never appearing, also I tried it with RegisterStartupScript but that doesn't work either...

该脚本永远不会出现,我也尝试使用RegisterStartupScript,但这不起作用......

In my HTML page I have included the following at the top: <%@ Page Language="C#" CodeBehind="Dashboard.aspx.cs" %>

在我的HTML页面中,我在顶部包含以下内容:<%@ Page Language =“C#”CodeBehind =“Dashboard.aspx.cs”%>

Can anyone spot the issue(s)? Any help is very much appreciated!

有人能发现问题吗?很感谢任何形式的帮助!

1 个解决方案

#1


1  

The point is, the last argument in RegisterClientScriptBlock method is named addScriptTags and since you have set it to true, you should remove the script tag and let C# add those tags for you:

重点是,RegisterClientScriptBlock方法中的最后一个参数名为addScriptTags,因为您已将其设置为true,所以应删除脚本标记并让C#为您添加这些标记:

    int[,] allSegments = GetAllSegments();

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = serializer.Serialize(allSegments);
    string script = String.Format("var allSegments={0};", json);
    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script, true);

or just set addScriptTags to false:

或者只是将addScriptTags设置为false:

        int[,] allSegments = GetAllSegments();

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(allSegments);
        string script = String.Format("<script type=\"text/javascript\">var allSegments={0};</script>", json);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script, false);

The other minor change I have made is just to add the semicolon to the end of your js code.

我做的另一个小改动只是将分号添加到js代码的末尾。

#1


1  

The point is, the last argument in RegisterClientScriptBlock method is named addScriptTags and since you have set it to true, you should remove the script tag and let C# add those tags for you:

重点是,RegisterClientScriptBlock方法中的最后一个参数名为addScriptTags,因为您已将其设置为true,所以应删除脚本标记并让C#为您添加这些标记:

    int[,] allSegments = GetAllSegments();

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = serializer.Serialize(allSegments);
    string script = String.Format("var allSegments={0};", json);
    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script, true);

or just set addScriptTags to false:

或者只是将addScriptTags设置为false:

        int[,] allSegments = GetAllSegments();

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(allSegments);
        string script = String.Format("<script type=\"text/javascript\">var allSegments={0};</script>", json);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script, false);

The other minor change I have made is just to add the semicolon to the end of your js code.

我做的另一个小改动只是将分号添加到js代码的末尾。