[英]Reload Javascript Gage Only, not the Whole Page
I have a Javascript Gage that displays a percent and refreshed every 5 seconds in an ASPX page. 我有一个Javascript Gage,它显示一个百分比,并在ASPX页面中每5秒刷新一次。 As of now I am refreshing the whole page.
截至目前,我正在刷新整个页面。 How can I refresh just the Javascript Gage?
如何仅刷新Javascript Gage? Refreshing the whole page is not good practice.
刷新整个页面不是一个好习惯。 The Gage is inside div gg11.
量具在div gg11内部。 I am new at javascript, how can I achieved this?
我是javascript新手,如何实现此目标?
ASPX ASPX
<div id="gg11" class="gauge"></div>
<meta http-equiv="refresh" content="5; URL=http://localhost:63738/main.aspx">
ASPX.cs ASPX.cs
protected void Page_Load(object sender, EventArgs e)
{
JavaScriptMethod();
}
protected void JavaScriptMethod()
{
Calculations();
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var gg1 = new JustGage({");
sb.Append("id: \"gg11\",");
sb.Append("donut: 0,");
sb.Append("title: \"LAKE WALES\",");
sb.Append("titleFontColor: \"#000000\",");
sb.AppendFormat("value: {0},", percent);
sb.Append("min: 0,");
sb.Append("max: 100,");
sb.Append("labelFontColor: \"#000000\",");
sb.Append("humanFriendlyDecimal: 1,");
sb.Append("decimals: 1,");
sb.Append("symbol: \"%\",");
sb.Append("startAnimationType : \"bounce\",");
sb.Append("refreshAnimationType: \"bounce\",");
sb.Append("startAnimationTime: 1000,");
sb.Append("gaugeWidthScale: 1.2,");
sb.Append("customSectors: [{color: \"#ff0000\",lo: 0,hi: 79}, {color: \"#ffa500\",lo: 80,hi: 89}, {color: \"#1eae1e\",lo: 90,hi: 100}],");
sb.Append("counter: true");
sb.Append("});");
sb.Append("</script>");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", sb.ToString(), false);
}
Use a combination of JavaScript, jQuery and ASP.NET AJAX Page Methods, like this: 结合使用JavaScript,jQuery和ASP.NET AJAX页面方法,如下所示:
Markup: 标记:
<div id="gg11" class="gauge"></div>
JavaScript: JavaScript的:
$(document).ready(function() {
setInterval(doAjax, 5000);
});
function doAjax() {
$.ajax({
type: "POST",
url: "YourPageName.aspx/GetGage",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
if (result.hasOwnProperty("d")) {
// The .d is part of the result so reference it
// to get to the actual JSON data of interest
// Put result into DIV
$('#gg11').html(result.d);
}
else {
// No .d; so just use result
// Put result into DIV
$('#gg11').html(result);
}
}
});
}
Note: You will need to change the name of
YourPageName.aspx
to the name of your .aspx page.注意:您需要将
YourPageName.aspx
的名称更改为.aspx页的名称。 Also, the.d
syntax was an anti-XSS protection put in by Microsoft in the ASP.NET 3.5 release of ASP.NET AJAX;另外,
.d
语法是Microsoft在ASP.NET AJAX的ASP.NET 3.5版本中.d
的反XSS保护; therefore the check to see if the .d property is there or not.因此检查以查看.d属性是否存在。
Code-behind: 代码隐藏:
[WebMethod]
public static string GetGage()
{
Calculations();
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var gg1 = new JustGage({");
sb.Append("id: \"gg11\",");
sb.Append("donut: 0,");
sb.Append("title: \"LAKE WALES\",");
sb.Append("titleFontColor: \"#000000\",");
sb.AppendFormat("value: {0},", percent);
sb.Append("min: 0,");
sb.Append("max: 100,");
sb.Append("labelFontColor: \"#000000\",");
sb.Append("humanFriendlyDecimal: 1,");
sb.Append("decimals: 1,");
sb.Append("symbol: \"%\",");
sb.Append("startAnimationType : \"bounce\",");
sb.Append("refreshAnimationType: \"bounce\",");
sb.Append("startAnimationTime: 1000,");
sb.Append("gaugeWidthScale: 1.2,");
sb.Append("customSectors: [{color: \"#ff0000\",lo: 0,hi: 79}, {color: \"#ffa500\",lo: 80,hi: 89}, {color: \"#1eae1e\",lo: 90,hi: 100}],");
sb.Append("counter: true");
sb.Append("});");
sb.Append("</script>");
return sb.ToString();
}
Use the javascript setInterval
method to make an ajax call to your server where your sever page returns the HTML markup for the specific part you are interested in. Remove the meta tag which refreshes the whole page. 使用javascript
setInterval
方法对服务器进行Ajax调用,在服务器上,服务器页面将返回您感兴趣的特定部分的HTML标记。删除元标记以刷新整个页面。
Assuming you have jQuery loaded in your page 假设您在页面中加载了jQuery
$(function(){
var intId= setInterval(function(){
$("#gg11").load("getgauge.aspx");}, 5000);
});
This will keep sending an ajax request to the server page every 5 seconds! 这将继续每5秒将ajax请求发送到服务器页面! You should have the getgauge.aspx returns the HTML markup you want to show in the UI.
您应该让getgauge.aspx返回要在UI中显示的HTML标记。
If you are interested in only the changed value (whenever some value is changed in server), You may look into libraries like SignalR which makes realtime communication an easy task. 如果您只对更改的值感兴趣(无论何时服务器中的某些值发生更改),您都可以使用SignalR之类的库,这使实时通信成为一项轻松的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.