TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Pothi Sam
NA
335
50.4k
Need Jquery Call From C# Code Behind
Feb 19 2018 7:29 AM
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"_Default"
%>
<%@ Register Assembly=
"AjaxControlToolkit"
Namespace=
"AjaxControlToolkit"
TagPrefix=
"asp"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
<script src=
"//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"
type=
"text/javascript"
></script>
<script type=
"text/javascript"
src=
"//www.google.com/jsapi"
></script>
<script type=
"text/javascript"
>
google.load(
'visualization'
,
'1'
, { packages: [
'corechart'
] });
</script>
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$.ajax({
type:
'POST'
,
dataType:
'json'
,
contentType:
'application/json'
,
url:
'Default.aspx/GetData'
,
data:
'{}'
,
success:
function
(response) {
drawVisualization(response.d);
}
});
})
function
drawVisualization(dataValues) {
var
data =
new
google.visualization.DataTable();
data.addColumn(
'string'
,
'Column Name'
);
data.addColumn(
'number'
,
'Column Value'
);
for
(
var
i = 0; i < dataValues.length; i++) {
data.addRow([dataValues[i].ColumnName, dataValues[i].Value]);
}
new
google.visualization.PieChart(document.getElementById(
'visualization'
)).
draw(data, { title:
"Google Chart demo"
});
}
</script>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div id=
"visualization"
style=
"width: 600px; height: 350px;"
>
</div>
</form>
</body>
</html>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Web.Services;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
[WebMethod]
public
static
List<Data> GetData()
{
SqlConnection conn =
new
SqlConnection(
"Data Source=SPIDER;Initial Catalog=Northwind;Integrated Security=True"
);
DataSet ds =
new
DataSet();
DataTable dt =
new
DataTable();
conn.Open();
string
cmdstr =
"select top 5 Country, COUNT(CompanyName) [Total Suppliers] from [Suppliers] group by Country"
;
SqlCommand cmd =
new
SqlCommand(cmdstr, conn);
SqlDataAdapter adp =
new
SqlDataAdapter(cmd);
adp.Fill(ds);
dt = ds.Tables[0];
List<Data> dataList =
new
List<Data>();
string
cat=
""
;
int
val=0;
foreach
(DataRow dr
in
dt.Rows)
{
cat=dr[0].ToString();
val=Convert.ToInt32( dr[1]);
dataList.Add(
new
Data(cat, val));
}
return
dataList;
}
}
public
class
Data
{
public
string
ColumnName =
""
;
public
int
Value = 0;
public
Data(
string
columnName,
int
value)
{
ColumnName = columnName;
Value = value;
}
}
That is google pie Chart Example I need To Call pie Chart From Code Behind like
private
void
BindChart()
{
DataTable dt =
new
DataTable();
try
{
dt = GetData();
str.Append(@"<script type=*text/javascript*> google.load( *visualization*, *1*, {packages:[*corechart*]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data =
new
google.visualization.DataTable();
data.addColumn(
'string'
,
'Year'
);
data.addColumn(
'number'
,
'Sales'
);
data.addRows(
" + dt.Rows.Count + "
);");
for
(
int
i = 0; i <= dt.Rows.Count - 1; i++)
{
str.Append(
"data.setValue( "
+ i +
","
+ 0 +
","
+
"'"
+ dt.Rows[i][
"year"
].ToString() +
"');"
);
str.Append(
"data.setValue("
+ i +
","
+ 1 +
","
+ dt.Rows[i][
"Sales"
].ToString() +
") ;"
);
}
str.Append(
" var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));"
);
str.Append(
" chart.draw(data, {width: 550, height: 300, title: 'Company Performance',"
);
str.Append(
"hAxis: {title: 'Year', titleTextStyle: {color: 'green'}}"
);
str.Append(
"}); }"
);
str.Append(
"</script>"
);
lt.Text = str.ToString().Replace(
'*'
,
'"'
);
}
catch
{ }
}
}
Reply
Answers (
4
)
To show notification
Get 403.14 when access ASP.NET Core website deployed on IIS