0

I am trying to build a chart in my Asp.net web page and my code is as follows in Code behind file

            string WriteStr = "";
            WriteStr = "";
            WriteStr += "<?xml version='1.0' encoding='UTF-8'?>";
            if (DtIndsutry.Rows.Count > 0)
            {
                WriteStr += "<chart>";
                WriteStr += "<series>";
                for (int i = 0; i < DTTable.Rows.Count; i++)
                {

                    WriteStr += "<value xid='" + i + "'>";

                    string Data = DTTable.Rows[i]["Name"].ToString();

                    WriteStr += Data;
                    WriteStr += "</value>";
                }


                WriteStr += "</series>";
                WriteStr += "<graphs>";
                WriteStr += "<graph gid='1'>";
            }
            for (int i = 0; i < DTTable.Rows.Count; i++)
            {

                WriteStr += "<value xid='" + i + "'>";

                string percent= DTTable.Rows[i]["Percent"].ToString();

                WriteStr += percent;
                WriteStr += "</value>";
            }


            WriteStr += "</graph>";

            WriteStr += "</graphs>";
            WriteStr += "</chart>";

            Response.Write(WriteStr);

and in my aspx page

            <script type="text/javascript" language="javascript">

     function GetBarChart() {

        var RequestUrl = "../Client/ChartPage.aspx";

        var oi = new SWFObject("../amcolumn/amcolumn.swf", "CategoryRet", "1000", "280", "8", "#ffffff");
        oi.addVariable("path", "../amcolumn/");
        oi.addVariable("settings_file", encodeURIComponent("../amcolumn/amcolumn_settings_mf.xml"));
        oi.addParam("wmode", "opaque");
        oi.addVariable("data_file", encodeURIComponent(RequestUrl));
        oi.write("GetBarChart");
    }

        <div id="GetBarChart">
            <script language="javascript" type="text/javascript">
                GetBarChart();
            </script>
        </div>

Now My problem is when my Code behind contains some other codes and when i try to create this chart what happens is the data gets printed on the top of my web page in xml format which I dont want,

but when I create a seperate class just for the chart the chart displays fine. However the problem with the second approach is for every chart I have to create a new class which is also not recommended.

How can i acheive this? Can I access a method in class using javascript??

Thank you all

4

1 回答 1

0

您是否尝试过使用 Page.ClientScript.RegisterStartupScript() ?

string jsScript = null;

jsScript = "<script type='text/javascript'>" + Constants.vbCrLf;
jsScript += "function GetBarChart();" + Constants.vbCrLf;
jsScript += "{" + Constants.vbCrLf;
                /* add your logic here */
jsScript += "}" + Constants.vbCrLf;
jsScript += "</script>";

Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", jsScript);

您也可以参考此链接http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

于 2013-06-01T08:39:30.633 回答