方法:
通过使用.net自带组件ScriptManager、UpdatePanel、Timer、Label
实现功能:
实现了.net页面中label的局部自动定时刷新,避免了页面整体的刷新
前端:
<div><h1 style="color:#5FB878">当前人数:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="timerTest_Tick"></asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</h1></div>
后端:
protected void timerTest_Tick(object sender, EventArgs e)
{
labelout();
}
public void labelout()
{
String strCon = @"连接字符串";
SqlConnection conn = new SqlConnection(strCon);
string sqlStr = "select count(*) from worker";
SqlCommand sqlcmd = new SqlCommand(sqlStr, conn);
conn.Open();
object objstr = sqlcmd.ExecuteScalar();
conn.Close();
Label1.Text = objstr.ToString();
}