我尝试显示错误信息
我在 GridView 中有一个链接按钮..当我点击这个链接按钮和这个静态函数时我调用 highcharts..通过这个静态函数我获取数据然后通过 javascript 调用这个函数所以当我点击这个显示按钮图表但是当没有图表时它显示代码错误所以为此我想在没有图表时显示警告框..
public static function(int ID)
try
{
}
catch (Exception ex)
{
Response.Write("<script>alert('" + Server.HtmlEncode(ex.ToString()) + "')</script>");
}
我在上面尝试过,但显示错误消息
Error 3 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'
Error 2 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Response.get'
lbViewChart
是链接按钮 ...
查询
<script type="text/javascript">
var strArray = "[['sfdsdfLi', 9],['Kiwsdfi', 3],['Mixesdfd nuts', 1],['Oranges', 6],['Grapes (bunch)', 1]]";
$(function () {
$('[ID*=lbViewChart]').on('click', function () {
var row = $(this).closest('tr');
var Id = row.find('td')[0].firstChild.data;
var obj = {};
obj.ID = Id;
GetData(obj);
return false;
});
});
function GetData(obj) {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetVoiliations",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (result) {
alert(result.d);
alert('u');
//start
strArray = result.d;
var myarray = eval(strArray);
$('#container').highcharts({
chart: {
type: 'pie',
options3d: {
enabled: true,
alpha: 45
}
},
title: {
text: 'Contents of Highsoft\'s weekly fruit delivery'
},
subtitle: {
text: '3D donut in Highcharts'
},
plotOptions: {
pie: {
innerSize: 100,
depth: 45
}
},
series: [{
name: 'Delivered amount',
data: myarray
}]
});
//end
},
error: function (error) {
alert(error);
}
});
}
// });
</script>
有什么解决办法吗?
最佳答案
您不能直接在静态方法中访问 Server
而不是使用 System.Web.HttpContext.Current.Server
所以代码将是这样的:
System.Web.HttpContext.Current.Response.Write("<script>alert('" + System.Web.HttpContext.Current.Server.HtmlEncode(ex.ToString()) + "')</script>");
或者将 using System.Web;
包含到 using 部分,然后使用 HttpContext.Current.Server
更新:-
HttpContext.Current
是静态属性,因此您可以直接在静态方法中访问它。因此你可以像下面这样访问 .Server
和 .Response`:
System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
currentContext.Response.Write("<script>alert('" + currentContext.Server.HtmlEncode(ex.ToString()) + "')</script>");
关于javascript - try catch 静态函数 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914870/