0

I have a asp.net 3.5 web form, I am using several server side validators control, and I want that after the page is validated , the javascript code to print fires at submit button click event handler

I tried using OnClientClick but this will fire even the print page javascript even when page is not valid ,

How would I accomplish this , that the print show only when form is valid?

This is my code, thanks in advance

    <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


        }
rgvwed
  • 47
  • 1
  • 4
  • 11

2 Answers2

0

check the Page_IsValid in javascript function before print command.

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

      }
     else
     {
         // page is not validated.
     }

}
Jagz W
  • 855
  • 3
  • 12
  • 28
  • 1
    for more information, check this link http://stackoverflow.com/questions/1066857/determine-if-page-is-valid-in-javascript-asp-net – Jagz W Jun 12 '12 at 03:24
0

So do you want a Print button to show when the page is validated? If so:

Cant you create another server side button with an OnClientClick to fire the print action. This button can be made visible on validation in your above code.

astro boy
  • 1,410
  • 1
  • 11
  • 16
  • Yes, I need the dialog to show once the form was validated and was valid, the thing is my above screen doesn trigger the popup to print – rgvwed Jun 12 '12 at 03:52