简体   繁体   English

从updatepanel调用javascript

[英]call javascript from updatepanel

I'm using google charts to draw a set of charts changing chart type with a dropdownlist. 我正在使用Google图表绘制一组图表,并通过下拉列表更改图表类型。

however, after wrapping the dropdownlist in an updatepanel, the javascript is no longer being fired. 但是,将dropdownlist包装在updatepanel中之后,不再触发javascript。 first line in javascript is an alert to confirm. javascript中的第一行是要确认的警报。

the code that's supposed to fire the javascript: 应该触发javascript的代码:

protected void Unnamed_SelectedIndexChanged(object sender, EventArgs e) {
    DropDownList ddl = (DropDownList)sender;
    if (ddl.SelectedItem != null) {
        ClientScriptManager cs = Page.ClientScript;
        string script = String.Format("drawChart(\'{0}\',\'{1}\',\'{2}\',\'{3}\',\'{4}\');", chart.ClientID, ddl.SelectedValue.ToString(), "i en kage i gram", "string,NOGETTEKST;number,Mængde", "Mel,500;Sukker,500;Smør,100;Salt,32;Vand,400;Cola,150");
        cs.RegisterStartupScript(typeof(Page), "isActive", script, true);
    }
}

this worked fine before i put the updatepanel around the dropdownlist. 在我将updatepanel放在dropdownlist周围之前,这工作得很好。

<asp:UpdatePanel runat="server" ID="hejbamse" ChildrenAsTriggers="true">

<ContentTemplate>
    <asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed_SelectedIndexChanged" ID="ddl" AutoPostBack="true">
        <asp:ListItem Text="" />
        <asp:ListItem Text="LineChart" />
        <asp:ListItem Text="PieChart" />
        <asp:ListItem Text="BarChart" />
        <asp:ListItem Text="ColumnChart" />
    </asp:DropDownList>
    <div id="chart" style="width: 900px; height: 500px;" runat="server"></div>
</ContentTemplate>

i've tried different approaches using scriptmanager instead of clientscript inspired from different answers on SO, but with no luck. 我尝试使用scriptmanager而不是从SO的不同答案中获得灵感的clientscript尝试不同的方法,但是没有运气。

EDIT: 编辑:

replacing: 取代:

cs.RegisterStartupScript(typeof(Page), "isActive", script, true); 

with: 与:

ScriptManager.RegisterStartupScript(updatepanelid, updatepanelid.GetType(), Guid.NewGuid().ToString(), script, true);

fixied it. 解决它。

You can try put the script in a Literal, instead of 您可以尝试将脚本放入文字中,而不是

cs.RegisterStartupScript(typeof(Page), "isActive", script, true);

PS. PS。 I haven't test it. 我还没有测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM