我在C#asp.net项目中运行了一个foreach循环,该项目在服务器端运行。
在循环的每次迭代完成后,我想在客户端Web浏览器上更新文本框(consolebox.text),以便用户可以看到循环已完成。
它仅在函数完成后更新文本框,因此用户在整个foreach完成之前不会看到进度输出。下面是我的代码,我试过ajax updatepanels无济于事
protected void Button1_Click(object sender, EventArgs e)
{
consolebox.Text = "Please Wait........"+ Environment.NewLine;
foreach (var listBoxItem in serverlist.Items)
{
string send = listBoxItem.ToString();
DELETEPROFILE(send);
consolebox.Text += ("" + send + "........Complete" + Environment.NewLine);
}
}
答案 0 :(得分:0)
您可以通过网络服务完成。使用ajax开始运行,其他服务可以读取功能。
样品:
.aspx的
<script type = "text/javascript">
function ajaxCall(fsMathod) {
$.ajax({
type: "POST",
url: "Test.aspx/" + fsMathod,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
var TheTextBox = document.getElementById("<%=consolebox.ClientID%>");
TheTextBox.value = TheTextBox.value + response.d;
}
</script>
<body>
<form id="form1" runat="server">
<div style="margin:0 auto; width:20%">
Textbox: <asp:TextBox ID="consolebox" TextMode="MultiLine" runat="server"></asp:TextBox>
<br />
<input id="btnStartAsync" type="button" value="Start Async" onclick = "ajaxCall('startAsync');" />
<input id="btnReadAsync" type="button" value="Read Async" onclick = "ajaxCall('readAsync')" />
</div>
</form>
</body>
C#
static string CompletedItems = "";
[System.Web.Services.WebMethod]
public static string readAsync()
{
return "" + CompletedItems + "........Complete\n";
}
[System.Web.Services.WebMethod]
public static void startAsync()
{
asyncTask();
}
private static void asyncTask()
{
foreach (var listBoxItem in serverlist.Items)
{
string send = listBoxItem.ToString();
DELETEPROFILE(send);
//consolebox.Text += ("" + send + "........Complete" + Environment.NewLine);
CompletedItems += send + ",";
}
}