【问题标题】:execute javascript print after validation took place in asp.net web form在asp.net web表单中进行验证后执行javascript打印
【发布时间】:2012-06-14 22:02:02
【问题描述】:

我有一个 asp.net 3.5 网络表单,我正在使用多个服务器端验证器控件,我希望在验证页面后,在提交按钮单击事件处理程序时触发要打印的 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


        }

【问题讨论】:

    标签: c# asp.net validation


    【解决方案1】:

    在打印命令之前检查 javascript 函数中的 Page_IsValid。

    function PrintDocument()
    {
        if (Page_IsValid) {
            // call your print function here....
    
          }
         else
         {
             // page is not validated.
         }
    
    }
    

    【讨论】:

    【解决方案2】:

    那么您是否希望在验证页面时显示“打印”按钮?如果是这样:

    您不能使用 OnClientClick 创建另一个服务器端按钮来触发打印操作。这个按钮可以在你上面的代码中验证时可见。

    【讨论】:

    • 是的,一旦表单被验证并且有效,我需要显示对话框,问题是我上面的屏幕不会触发弹出窗口打印
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多