全部,從循環內更新UI線程:ASP.NET Webforms
我有一個Update Panel控件,它包含一個標籤控件。我也有一個定時器控件,它的間隔設置爲1秒。計時器控件假定每秒鐘都使用公共屬性的值設置標籤控件的文本,該屬性在循環的每次迭代後都會發生變化。
但是最終的功能是在整個循環完成後更新UI。我想知道需要完成/編碼哪些內容以確保標籤控件在循環的每次迭代後都使用_servername屬性的值進行更新?
繼承人我的代碼:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="tmr" runat="server" Interval="1000" ontick="tmr_Tick">
</asp:Timer>
<div>
<asp:UpdatePanel ID="udp1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tmr" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblInserts" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress runat="server" ID="udprg1" AssociatedUpdatePanelID="udp1"
DisplayAfter="10">
<ProgressTemplate>
<img src="media/ajaxloaderBlueLoading.gif" alt="ProgressImage" />
</ProgressTemplate>
</asp:UpdateProgress>
<div id="_asyncCallsMadeDiv"></div>
</div>
</form>
//後面的代碼
public string ServerName
{
get { return _serverName; }
set { _serverName = value;}
}
protected void tmr_Tick(object sender, EventArgs e)
{
lblInserts.Text = ServerName;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
//Loop through data
while((line = rdr.ReadLine()) != null)
{
string [] arrayline = line.Split(',');
ServerStatus s = new ServerStatus
{
ServerName = arrayline[0],
Purpose = arrayline[1],
Primary = arrayline[2],
Secondary = arrayline[3],
OS = arrayline[4],
MachineType = arrayline[5],
Comments = arrayline[6],
VMTools = arrayline[7],
TimeSettings = arrayline[8],
LastPatchDate = arrayline[9],
CARemoval = arrayline[10],
PLUpdate = arrayline[11],
DefaultGatewayChange = arrayline[12]
};
_serverName = arrayline[0];
}
您的aspx標記和代碼隱藏似乎幾乎彼此無關。根據下面的註釋,'tmrUdp1'應該是'tmr'並且調用'tmr_Tick'事件處理程序。但是,該事件處理程序更新aspx標記中不存在的標籤。我認爲應該是'ltlCount'而不是?你能開始修復你的aspx或代碼隱藏嗎? – 2012-04-02 19:14:11
@GuthMD和CheckRaise,爲錯別字道歉。也應該現在可讀。 – 2012-04-02 20:43:09
你能解釋一下btnUpload_Click處理程序嗎? 'line'從哪裏來?這裏的操作看起來很簡單,我期望整個循環可能在不到一秒鐘內完成。你希望它經常回傳到你的頁面? – 2012-04-03 13:17:52