简体   繁体   English

在回发时从服务器端调用javascript

[英]Call javascript from server side on postback

var r = {  
        init : function(){  
             r = Raphael("pie");
            //r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
            r.g.text(320, -330, "Message Status").attr({ "font-size": 20 });

            var pie = r.g.piechart(360, -180, 100,  <%= Session["uStats"] %>, { legend: [<%= Session["vkeyColor"] %>], colors: [<%= Session["vPieColor"] %>]  });
            pie.hover(function () {
                this.sector.stop();
                this.sector.scale(1.1, 1.1, this.cx, this.cy);
                if (this.label) {
                    this.label[0].stop();
                    this.label[0].scale(1.5);
                    this.label[1].attr({ "font-weight": 800 });
                }
            }, function () {
                this.sector.animate({ scale: [1, 1, this.cx, this.cy] }, 500, "bounce");
                if (this.label) {
                    this.label[0].animate({ scale: 1 }, 500, "bounce");
                    this.label[1].attr({ "font-weight": 400 });
                }
            });
            var r = Raphael("pie"),

                    data2 = [<%= Session["vProgressPercentage"] %>];
                    axisx = ["10%", "20%"];
            r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
             r.g.barchart(80, 25, 100, 320, data2, { stacked: true, colors: [<%= Session["vProgressColor"] %>,'#fff'] });
             axis2 = r.g.axis(94, 325, 280, 0, 100, 10, 1);
        }
        }
          window.onload = function () {
          r.init();
        };

The Javascript is executed when the page loads, I do partial postback using a update panel, the javascript is not executed during post back how to call it from server side. 当页面加载时执行Javascript,我使用更新面板进行部分回发,在回发期间不执行javascript如何从服务器端调用它。

  <asp:ScriptManager ID="smCharts" runat="server" />
   <script type="text/javascript" language="javascript">
             Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
             Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
             function BeginRequestHandler(sender, args) {
                 r.init();
             }
             function EndRequestHandler(sender, args) {
                 r.init();
             }

            </script>
        <asp:UpdatePanel runat="server" ID="Holder" OnLoad="messsagePercentStats" UpdateMode="Conditional">
            <ContentTemplate>

                <div id="pie" onclick="__doPostBack('Holder', '');" style="top: -125px; left: -20px; width: 610px; position: relative;
                    height: 389px;">
                </div>

This is what I am trying and the graph is not changed. 这就是我正在尝试的,图表没有改变。 it remains constant as in page load 它在页面加载时保持不变

protected void Timer_Tick(object sender, EventArgs args)
    {
        String MsgID = HttpContext.Current.Request.QueryString["MsgID"];
        int msgID = Convert.ToInt32(MsgID);
        BindGridViewUsers(msgID);

        ClientScript.RegisterStartupScript(this.GetType(), "loadpie", "r.init();");
        Holder.Update();
        }

Use ScriptManager.RegisterStartupScript to call a javascript function from an update panel; 使用ScriptManager.RegisterStartupScript从更新面板调用javascript函数; something like this: 这样的事情:

ScriptManager.RegisterStartupScript(this,this.GetType(),"key","javscriptfunction();" , false);

You could put this script inside a function: 您可以将此脚本放在函数中:

function foo() {
    ...
}

which will be executed upon DOM load: 这将在DOM加载时执行:

window.onload = function () {
    foo();
};

and upon server side postback from an UpdatePanel: 并在服务器端从UpdatePanel回发:

ScriptManager.RegisterStartupScript(this, this.GetType(), "foo", "foo();", true);

window.onload does not work after the page has already loaded. 页面已加载后, window.onload不起作用。 Use 使用

$(function(){ r.init();})

instead. 代替。

You could also do 你也可以这样做

Response.Write("<script language='javascript'>alert('I'm an alert');</script>");

Just put your code in instead of the alert . 只需输入代码而不是alert You might be able to call the function from there, but I haven't tested that. 您可以从那里调用该函数,但我没有测试过。

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

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