0

私はasp.net 3.5 Webフォームを持っており、いくつかのサーバー側バリデーターコントロールを使用しています.ページが検証された後、印刷する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


        }
4

2 に答える 2

0

ページが検証されたときに [印刷] ボタンを表示しますか? もしそうなら:

OnClientClick を使用して別のサーバー側ボタンを作成して、印刷アクションを起動することはできません。このボタンは、上記のコードの検証時に表示することができます。

于 2012-06-12T03:31:00.030 に答える
0

印刷コマンドの前に、javascript 関数で Page_IsValid を確認してください。

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

      }
     else
     {
         // page is not validated.
     }

}
于 2012-06-12T03:23:17.727 に答える