通过c#将值设置为jQuery变量

本文关键字:jQuery 变量 设置 通过 | 更新日期: 2023-09-27 18:24:56

我想在我的网页上创建一个饼图。我从数据库中读取数据,并将它们设置在两个变量creditremain_cost中。

我想显示一个有这两个值的饼图。我发现了一个由DevExpress公司创建的饼图。

这是我的asp.net代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>chart</title>
    <meta charset="utf-8" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/globalize/0.1.1/globalize.min.js"></script>
        <script type="text/javascript" src="http://cdn3.devexpress.com/jslib/14.1.7/js/dx.chartjs.js"></script>
        <script>
            var pieChartDataSource = [
                { category: 'Credit', value: 44 },
                { category: 'reamaincost', value: 34 },
            ];
            $(function () {

                $("#pieChartContainer").dxPieChart({
                    dataSource: pieChartDataSource,
                    series: {
                        argumentField: 'category',
                        valueField: 'value',
                        label: {
                            visible: true,
                            connector: {
                                visible: true
                            }
                        }
                    },
                    tooltip: {
                        enabled: true,
                        percentPrecision: 2,
                        customizeTooltip: function (value) {
                            return {
                                text: value.percentText
                            };
                        }
                    },
                    title: {
                        text: 'user over view'
                    },
                    legend: {
                        horizontalAlignment: 'center',
                        verticalAlignment: 'bottom'
                    }
                });
            });
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:Literal ID="credittxt" runat="server" /> <br /> <asp:Literal ID="remain_costtxt" runat="server" />
            <asp:TextBox runat="server" ClientIDMode="Static" ID="txt" />
        </div>
    <div id="pieChartContainer" style="height:400px; max-width:700px; margin: 0 auto"></div>
    </div>
    </form>
</body>
</html>

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet1.usersDataTable ousersDataTable = new DataSet1.usersDataTable();
    DataSet1TableAdapters.usersTableAdapter ousersTableAdapter = new DataSet1TableAdapters.usersTableAdapter();
    ousersTableAdapter.FillBysip(ousersDataTable, "my_data");
    DataSet1.usersRow ousersRow = ousersDataTable[0];
    string credit = ousersRow.credit.ToString();
    string remain_cost = ousersRow.schduled_cost.ToString();
    credittxt.Text = credit;
    remain_costtxt.Text = remain_cost;
    txt.Text = credit;
}

数据成功地出现在文字和文本框控件中。

我的问题是,我想在jQuery中将c#中的credit值设置为credit值,并将c#中的retain_cost设置为jQuery中的remain_cost变量。

请帮帮我。

通过c#将值设置为jQuery变量

</body> 之前

<script>
           credit ='<%getCredit()%>';
           remain_cost ='<%getRemainCost()%>';
</script>

在你的代码隐藏文件:

string credit, remain_cost;
public getCredit(){return credit ;}
public getRemainCost(){return remain_cost ;}
protected void Page_Load(object sender, EventArgs e)
{
 credit = ousersRow.credit.ToString();
 remain_cost = ousersRow.schduled_cost.ToString();
}

您可以这样做:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet1.usersDataTable ousersDataTable = new DataSet1.usersDataTable();
    DataSet1TableAdapters.usersTableAdapter ousersTableAdapter = new           DataSet1TableAdapters.usersTableAdapter();
     ousersTableAdapter.FillBysip(ousersDataTable, "my_data");
     DataSet1.usersRow ousersRow = ousersDataTable[0];
   string credit = ousersRow.credit.ToString();
  string remain_cost = ousersRow.schduled_cost.ToString();
   credittxt.Text = credit;
   remain_costtxt.Text = remain_cost;
   txt.Text = credit;

Page.RegisterClientScriptBlock("MyScript","<SCRIPT Language='JavaScript'>credit = " +credut +"; _cost = " + cost +";</SCRIPT>");
}