0

在我的 ASP.NET 页面上,我在 asp:UpdatePanel 中使用 Google 可视化图表。用户从两个 asp:DropDownLists 中选择他们想要查看的图表,然后我的 C# 代码生成 JavaScript,然后我调用 ScriptManager.RegisterStartipScript(...)。当页面第一次加载第一个默认图表(即第一次调用 RegisterStartupScript)时,我可以从 View Source 查看 javascript。只有在回发时我才得到一个空白图表,然后当我转到查看源代码时,该页面没有收到新的 JavaScript,并且它仍然具有来自第一个页面加载的旧的默认 JavaScript。这是奇怪的行为。如果我使用完全相同的代码,但将我的 Google Chart 代码替换为 alert(...); 然后 alert() 每次都会触发,当我查看源代码时,脚本就在那里。我'在这里。下面是我的代码,任何帮助都将不胜感激,我让其他人看过这个,我们都很难过。仅供参考:如果我删除所有 UpdatePanel 和相关项目(ScriptManager 和 UpdateProgress)并使用 ClientScript.RegisterStartupScript(),那么一切正常,我在我的页面上获得了新的 JavaScript 代码,并且新图表按原样显示。

<asp:ScriptManager ID="ScriptMgr" runat="server" />

<asp:UpdatePanel ID="UpdatePanelData" runat="server">
    <ContentTemplate> 
        <p>
            <asp:DropDownList ID="PlotList" runat="server" AutoPostBack="true" Width="500px" 
                onselectedindexchanged="PlotList_SelectedIndexChanged"></asp:DropDownList>
            <asp:DropDownList ID="RangeList" runat="server" Width="125px" 
                style="float:right;" AutoPostBack="True" 
                onselectedindexchanged="RangeList_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
            <asp:Label ID="PlotDescription" runat="server" Width="100%"></asp:Label> 
        </p>
        <div id="chart_material" class="GoogleChart" style="width:100%;height:450px;"></div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="RangeList" EventName="SelectedIndexChanged" />
        <asp:AsyncPostBackTrigger ControlID="PlotList" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

 <asp:UpdateProgress ID="UpdateProgressData" runat="server" DisplayAfter="500">
    <ProgressTemplate>
        <div class="LoadingPanel">
            <asp:Image ID="LoadingImage" runat="server" ImageUrl="~/Images/loading_2.gif" AlternateText="Loading ..." ToolTip="Loading ..." style="padding: 10px;position:fixed;top:45%;left:50%;" />
        </div>
    </ProgressTemplate>
</asp:UpdateProgress>

我的 C# 代码看起来像这样。从两个 DropDownList 事件调用函数 DisplayPlot,每个列表作为“控件”传入。

private void DisplayPlot(Control control)
    {
        PlotInformation Plot = new PlotInformation(ChartDivID);

        double Range = Convert.ToDouble(RangeList.SelectedValue);

        string JavaScript = Plot.GetPlotScript(PlotList.SelectedItem.Text, Range);

        PlotDescription.Text = Plot.GetDataPlotDescription(PlotList.SelectedItem.Text);

        //string TestScript = "<script type=\"text/javascript\">\n\talert('" + PlotList.SelectedItem.Text + "');\n</script>";

        ScriptManager.RegisterStartupScript(control, control.GetType(), ScriptKey, JavaScript, false);
        //ScriptManager.RegisterStartupScript(control, this.GetType(), ScriptKey, JavaScript, false);

        //if (!ClientScript.IsStartupScriptRegistered(ScriptKey))
        //    ClientScript.RegisterStartupScript(this.GetType(), ScriptKey, JavaScript);
    }
4

1 回答 1

0

我知道这已经晚了,但我在同一个错误上花了很多时间。

尝试改变:

ScriptManager.RegisterStartupScript(control, control.GetType(), ScriptKey, 
  JavaScript, false);

ScriptManager.RegisterStartupScript(UpdatePanelData, UpdatePanelData.GetType(), ScriptKey, 
  JavaScript, false);
于 2016-04-15T15:49:27.873 回答