利用Asp.Net回調(diào)機(jī)制實(shí)現(xiàn)進(jìn)度條
更新時(shí)間:2009年01月09日 21:59:31 作者:
本文將利用Asp.Net的回調(diào)機(jī)制使用Js實(shí)現(xiàn)一個(gè)簡(jiǎn)易進(jìn)度條
其效果如下:

首先,在HTML文檔中加入如下代碼:
<div>
<table class="statusTable">
<tr>
<td id="progress1">
</td>
<td id="progress2">
</td>
<td id="progress3">
</td>
<td id="progress4">
</td>
<td id="progress5">
</td>
<td id="progress6">
</td>
<td id="progress7">
</td>
<td id="progress8">
</td>
<td id="progress9">
</td>
<td id="progress10">
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="0"></asp:Label>
</div>
<div>
<input id="btnRequest" type="button" value="請(qǐng)求" onclick="Request()" />
<input id="btnStop" type="button" value="停止" disabled="disabled" onclick="Stop()" />
</div>
Js部分加入如下Js代碼:
<script language="javascript" type="text/javascript">
var idx = 0;
var progressTimer;
var progressInterval = 1000;
function Request()
{
document.getElementById("btnStop").disabled = "";
document.getElementById("btnRequest").disabled = "disabled";
if(idx >= 10)
{
Clear();
return;
}
var arg = idx;
<%= ClientScript.GetCallbackEventReference(this, "arg", "GetMsgBack",null) %>;
idx++;
progressTimer = setTimeout('Request()',progressInterval);
}
function GetMsgBack(result)
{
document.getElementById('progress'+idx).style.backgroundColor = 'blue';
var status = Number(result) * 10;
document.getElementById("Label1").innerHTML = status + "%";
}
function Stop()
{
clearTimeout(progressTimer);
Clear();
}
function Clear()
{
idx = 0;
document.getElementById("btnStop").disabled = "disabled";
document.getElementById("btnRequest").disabled = "";
document.getElementById("Label1").innerHTML = "0";
for (var i = 1; i <= 10; i++)
document.getElementById('progress' + i).style.backgroundColor = 'transparent';
}
</script>
css樣式文件中加入如下代碼:
.statusTable
{
width:100px;
border:solid 1px #000000;
padding-bottom:0px;
margin-bottom:0px;
}
.statusTable td
{
height:20px;
}
Asp.Net服務(wù)端實(shí)現(xiàn)回調(diào)代碼如下:
public partial class DigitStatus : System.Web.UI.Page, ICallbackEventHandler
{
private string AspEventArgs;
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaiseCallbackEvent(string EventArgs)
{
AspEventArgs = EventArgs;
}
public string GetCallbackResult()
{
int i = Convert.ToInt32(AspEventArgs);
i++;
return i.ToString();
}
}
這樣,一個(gè)簡(jiǎn)易的進(jìn)度條就實(shí)現(xiàn)了,運(yùn)行即可得到如頁(yè)首處的效果。

首先,在HTML文檔中加入如下代碼:
<div>
<table class="statusTable">
<tr>
<td id="progress1">
</td>
<td id="progress2">
</td>
<td id="progress3">
</td>
<td id="progress4">
</td>
<td id="progress5">
</td>
<td id="progress6">
</td>
<td id="progress7">
</td>
<td id="progress8">
</td>
<td id="progress9">
</td>
<td id="progress10">
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="0"></asp:Label>
</div>
<div>
<input id="btnRequest" type="button" value="請(qǐng)求" onclick="Request()" />
<input id="btnStop" type="button" value="停止" disabled="disabled" onclick="Stop()" />
</div>
Js部分加入如下Js代碼:
<script language="javascript" type="text/javascript">
var idx = 0;
var progressTimer;
var progressInterval = 1000;
function Request()
{
document.getElementById("btnStop").disabled = "";
document.getElementById("btnRequest").disabled = "disabled";
if(idx >= 10)
{
Clear();
return;
}
var arg = idx;
<%= ClientScript.GetCallbackEventReference(this, "arg", "GetMsgBack",null) %>;
idx++;
progressTimer = setTimeout('Request()',progressInterval);
}
function GetMsgBack(result)
{
document.getElementById('progress'+idx).style.backgroundColor = 'blue';
var status = Number(result) * 10;
document.getElementById("Label1").innerHTML = status + "%";
}
function Stop()
{
clearTimeout(progressTimer);
Clear();
}
function Clear()
{
idx = 0;
document.getElementById("btnStop").disabled = "disabled";
document.getElementById("btnRequest").disabled = "";
document.getElementById("Label1").innerHTML = "0";
for (var i = 1; i <= 10; i++)
document.getElementById('progress' + i).style.backgroundColor = 'transparent';
}
</script>
css樣式文件中加入如下代碼:
.statusTable
{
width:100px;
border:solid 1px #000000;
padding-bottom:0px;
margin-bottom:0px;
}
.statusTable td
{
height:20px;
}
Asp.Net服務(wù)端實(shí)現(xiàn)回調(diào)代碼如下:
public partial class DigitStatus : System.Web.UI.Page, ICallbackEventHandler
{
private string AspEventArgs;
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaiseCallbackEvent(string EventArgs)
{
AspEventArgs = EventArgs;
}
public string GetCallbackResult()
{
int i = Convert.ToInt32(AspEventArgs);
i++;
return i.ToString();
}
}
這樣,一個(gè)簡(jiǎn)易的進(jìn)度條就實(shí)現(xiàn)了,運(yùn)行即可得到如頁(yè)首處的效果。
您可能感興趣的文章:
- Asp.net基于ajax和jquery-ui實(shí)現(xiàn)進(jìn)度條
- ASP.NET實(shí)現(xiàn)進(jìn)度條效果
- asp.net mvc 實(shí)現(xiàn)文件上傳帶進(jìn)度條的思路與方法
- asp.net文件上傳帶進(jìn)度條實(shí)現(xiàn)案例(多種風(fēng)格)
- asp.net單文件帶進(jìn)度條上傳的解決方案
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- asp.net 生成靜態(tài)頁(yè)時(shí)的進(jìn)度條顯示
- ASP.NET技巧:教你制做Web實(shí)時(shí)進(jìn)度條
- ASP.NET?MVC使用jQuery?ui的progressbar實(shí)現(xiàn)進(jìn)度條
相關(guān)文章
asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼
asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-12-12
Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法
Image/ImageButton服務(wù)器控件顯示本地的圖片,實(shí)現(xiàn)思路是數(shù)據(jù)庫(kù)中存放了圖片的相對(duì)地址,讀取數(shù)據(jù)庫(kù)中的地址,用控件加載顯示圖片。具體實(shí)現(xiàn)步驟大家參考下本文2017-08-08
ASP.NET中JQuery+AJAX調(diào)用后臺(tái)
這篇文章主要介紹了ASP.NET中JQuery+AJAX調(diào)用后臺(tái)的相關(guān)資料,需要的朋友可以參考下2016-03-03
asp.net讀取磁盤(pán)文件、刪除實(shí)例代碼
這篇文章介紹了asp.net讀取磁盤(pán)文件、刪除實(shí)例代碼,有需要的朋友可以參考一下2013-10-10
ASP.NET簡(jiǎn)單實(shí)現(xiàn)注銷(xiāo)功能
這篇文章主要介紹了如何ASP.NET簡(jiǎn)單實(shí)現(xiàn)注銷(xiāo)功能的方法以及簡(jiǎn)單示例,有需要的小伙伴可以參考下。2015-07-07
Centos7+Docker+Jenkins+ASP.NET Core 2.0自動(dòng)化發(fā)布與部署的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Centos7+Docker+Jenkins+ASP.NET Core 2.0自動(dòng)化發(fā)布與部署的相關(guān)資料,文中通過(guò)示例代碼及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2018-05-05

