如何编写返回累计总数的SQL Server存储过程并在ASP.NET中更新标签?
ASP.NET(C#)页面实现:绑定到Label + 定时刷新 + 自动重置
推荐用AJAX局部刷新的方式,避免整页刷新的生硬体验。
1. 前端页面代码
在你的ASPX页面中添加Label控件和定时刷新的脚本:
<asp:Label ID="lblCumulativeTotal" runat="server" Text="累计包装总数: 0"></asp:Label> <!-- 引入jQuery,可替换为项目中已有的版本 --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // 页面加载时先获取一次累计值 $(document).ready(function() { loadCumulativeTotal(); }); // 每1分钟(60000毫秒)刷新一次数据 setInterval(loadCumulativeTotal, 60000); function loadCumulativeTotal() { // 替换成你的SeriesMasterId,可从Session、配置或页面参数获取 const seriesMasterId = '<%= YourSeriesMasterIdValue %>'; $.ajax({ url: '<%= ResolveUrl("~/GetCumulativeTotal.ashx") %>', type: 'GET', data: { seriesMasterId: seriesMasterId }, success: function(response) { $('#<%= lblCumulativeTotal.ClientID %>').text('累计包装总数: ' + response.total); }, error: function() { $('#<%= lblCumulativeTotal.ClientID %>').text('累计值加载失败'); } }); } </script>
2. 后台一般处理程序(ASHX)
创建一个名为GetCumulativeTotal.ashx的文件,用来处理AJAX请求并调用存储过程:
using System; using System.Data; using System.Data.SqlClient; using System.Web; public class GetCumulativeTotal : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; // 获取前端传入的SeriesMasterId if (!int.TryParse(context.Request.QueryString["seriesMasterId"], out int seriesMasterId)) { context.Response.Write("{\"total\": 0}"); return; } // 从配置文件读取数据库连接字符串 string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["YourDbConnection"].ConnectionString; int total = 0; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("dbo.uspGetPackCumulativeTotal", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@SeriesMasterId", seriesMasterId); cmd.Parameters.AddWithValue("@ResetManually", 0); // 启用自动重置逻辑 using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { total = reader.GetInt32(reader.GetOrdinal("TotalCumulativePacked")); } } } } // 返回JSON格式的结果 context.Response.Write($"{{\"total\": {total}}}"); } public bool IsReusable => false; }
备选方案:整页刷新
如果你更倾向于整页刷新,也可以用Meta标签实现定时刷新,然后在后台Page_Load事件中绑定Label:
<!-- 在页面头部添加定时刷新,60秒刷新一次 --> <meta http-equiv="refresh" content="60"> <asp:Label ID="lblCumulativeTotal" runat="server"></asp:Label>
后台代码:
protected void Page_Load(object sender, EventArgs e) { BindCumulativeTotal(); } private void BindCumulativeTotal() { int seriesMasterId = 123; // 替换成实际的SeriesMasterId string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["YourDbConnection"].ConnectionString; int total = 0; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("dbo.uspGetPackCumulativeTotal", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@SeriesMasterId", seriesMasterId); cmd.Parameters.AddWithValue("@ResetManually", 0); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { total = reader.GetInt32(reader.GetOrdinal("TotalCumulativePacked")); } } } } lblCumulativeTotal.Text = $"累计包装总数: {total}"; }
内容的提问来源于stack exchange,提问作者user7422837
相关产品推荐
相关产品推荐

