C# 在asp.net web表单中进行验证后执行javascript打印

C# 在asp.net web表单中进行验证后执行javascript打印,c#,asp.net,validation,C#,Asp.net,Validation,我有一个asp.NET3.5Web表单,我正在使用几个服务器端验证程序控件,我希望在页面验证后,在提交按钮单击事件处理程序时触发要打印的javascript代码 我尝试使用OnClientClick,但即使页面无效,也会触发打印页面javascript 我该如何做到这一点,即打印仅在表单有效时显示 这是我的代码,提前谢谢 <asp:Button ID="btnAction" runat="server" OnClick="btnAction_Click" Text="Sub

我有一个asp.NET3.5Web表单,我正在使用几个服务器端验证程序控件,我希望在页面验证后,在提交按钮单击事件处理程序时触发要打印的javascript代码

我尝试使用OnClientClick,但即使页面无效,也会触发打印页面javascript

我该如何做到这一点,即打印仅在表单有效时显示

这是我的代码,提前谢谢

    <asp:Button ID="btnAction" runat="server" OnClick="btnAction_Click"
    Text="Submit" />

    protected void btnAction_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                // define SMTP client

                SmtpClient smtp = new SmtpClient();


                //create the mail message
                MailMessage message = new MailMessage();

                //From address will be given as a MailAddress Object

                //To address collection of MailAddress

                message.To.Add("########");

                //CC and BCC optional
                //message.CC.Add("");

                // Change to false to not include HTML

                message.IsBodyHtml = true;

                message.Body += "<h2>info goes here</h2></br></br>";

                //send the message

                try
                {
                    smtp.Send(message);
                }

                catch (System.Net.Mail.SmtpException ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }




                Page.ClientScript.RegisterStartupScript(this.GetType(),
    "OnPrintPage", "window.print();", true);

                  Response.Redirect("Confirmation.aspx?value=" +
    Server.UrlEncode(confirmationNumber));

                //Passing in session
                //Session["sessionname"] = confirmationNumber;


            }
            //End if Page is Valid Block


        }

受保护的无效b选项\u单击(对象发送方,事件参数e)
{
如果(第页有效)
{
//定义SMTP客户端
SmtpClient smtp=新SmtpClient();
//创建邮件消息
MailMessage=新的MailMessage();
//发件人地址将作为MailAddress对象提供
//处理邮件地址集合的步骤
message.To.添加(“#######””;
//抄送和密件抄送可选
//message.CC.Add(“”);
//更改为false以不包含HTML
message.IsBodyHtml=true;
message.Body+=“此处显示信息

”; //发送消息 尝试 { smtp.Send(message); } 捕获(System.Net.Mail.SmtpException-ex) { 抛出新异常(例如Message.ToString()); } 捕获(例外情况除外) { 抛出新异常(例如Message.ToString()); } Page.ClientScript.RegisterStartupScript(this.GetType(), “OnPrintPage”、“window.print();”,true); 重定向(“Confirmation.aspx?value=”+ Server.UrlEncode(确认号)); //交接班 //会话[“会话名称”]=确认号; } //如果页面是有效块,则结束 }
在打印命令之前,检查javascript函数中的页面是否有效

function PrintDocument()
{
    if (Page_IsValid) {
        // call your print function here....

      }
     else
     {
         // page is not validated.
     }

}

那么,是否希望在验证页面时显示打印按钮?如果是:


你不能用OnClientClick创建另一个服务器端按钮来启动打印操作吗。在上述代码中验证时可以看到此按钮。

有关更多信息,请检查此链接是的,我需要对话框显示表单验证和有效后,问题是上面的屏幕不会触发弹出窗口打印