简体   繁体   English

仅重新加载Javascript Gage,而不重新加载整个页面

[英]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.

相关问题 使用 Javascript(或 JQuery)重新加载 SWF Object 而无需重新加载整个页面(在 Ajax 调用之后) - Reload SWF Object using Javascript (or JQuery) without re-loading whole page (after Ajax call) 如何在WPF中重新加载(重置)整个页面? - How to reload (reset) the whole page in WPF? 带有Ajax的Asp.Net页面每次都重新加载整个页面,为什么? - Asp.Net page with Ajax reload the whole page everytime, why? 绑定数据仅刷新控件,而不刷新整个页面 - Databind to refresh only a control, not a whole page Blazor(详细)页面重新加载导致 javascript 错误 - Blazor (detail) page reload causes javascript errors 从 javascript POST 重新加载剃刀页面 - Reload razor page from javascript POST 锚onclick不应该重新加载页面但触发javascript? - Anchor onclick should not reload the page but trigger javascript? 有哪些选项可以不为 ASP .NET Core Razor 页面上的文件浏览器重新加载整个页面? - What options are there to not reload whole page for a file browser on ASP .NET Core Razor page? 在aspx Webform中从Javascript调用C#函数,然后重新加载页面 - Call C# function from Javascript in aspx webform and then reload page 如何仅导出gridview内容而不导出整个页面(pdf)? - How to export only gridview content but not the whole page in pdf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM