C#先执行一段sql等后台操作后再提示是否后续操作confrim

时间:2023-03-08 22:17:24

应用场景:例如选择一个单据号打击打印后先去数据库检索是否有打打印过,如果有则提示,已打印,是否再打

如果没有则不提示,直接进行打印。

实现原理:多做一个隐藏按钮去实现打印功能,页面上的打印按钮则进行数据库的后台操作,跟据取出的数据

可以在后台直接调用打印,或用脚本调用前台confirm

代码Demo:

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
//sql
string result = TextBox1.Text.Trim(); if (!string.IsNullOrEmpty(result))
{
Page.ClientScript.RegisterStartupScript(GetType(),"", "<script>CheckPrint('单号:" + result + "已打印,是否继续打印?')</script>");
}
else
{
Button2_Click(sender,e);
}
} protected void Button2_Click(object sender, EventArgs e)
{
this.Response.Write("执行了!!!!");
}
}
}

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/jscript">
function CheckPrint(obj) {
var IsHave = confirm(obj); if (IsHave)//调用后台方法
{
document.getElementById("Button2").click();
}
}
</script> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="print" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" Width="1px"/> </div>
</form>
</body>
</html>