Validating form fields before printing with client side scripts

Posted on 2007-04-03
Last Modified: 2013-11-26
I am creating a webform using Visual Studio 2005 with 2.0. I am using a single button control to postback the data on the form so that I can e-mail it to specified recipients. I am also using the same button to open a print dialog by using the OnClientClick event. I have been able to get the button to both postback and open the print dialog, but I also want the button to validate the page by using Page.IsValid before the users can even print it off. Right now, they can print without having to fill out all of the required fields. Originally, this is what I started with:

OnClick="printButton_Click" OnClientClick="window.print()" />  (this worked fine but did not validate before printing)

This is what I have come up with to do the validation, but I can't get it to work properly:

OnClick="printButton_Click" OnClientClick="if (Page.IsValid = true) {window.print()}" />
Question by:odie532
LVL 33

Expert Comment

ID: 18845690
use two == instead of one

if (Page.IsValid == true)

Author Comment

ID: 18845896
I changed the = to have two as opposed to one, but still no luck. Here is what I have now:

OnClick="printButton_Click" OnClientClick="if (Page.IsValid == true) {window.print()}" />

It doesn't seem to be sending the e-mails anymore either since I made that change. For good measure, here is the code from the printButton_click event:

protected void printButton_Click(object sender, EventArgs e)

        if (Page.IsValid)
            string supplierName = supplierNameTextBox.Text;
            string returnAddress = returnAddressTextBox.Text;
            string pcName = pcNameTextBox.Text;
            string pcPhone = pcPhoneTextBox.Text;
            string pcFax = pcFaxTextBox.Text;
            string pcEmail = pcEmailTextBox.Text;
            string partNumber = partNumberTextBox.Text;
            string partDescription = partDescriptionTextBox.Text;
            string poNumber = poNumberTextBox.Text;
            string poLineNumber = poLineNumberTextBox.Text;
            string supplierNotificationDate = supplierNotificationTextBox.Text;
            string matReturnAuth = returnMatAuthTextBox.Text;
            string reasonReturned = reasonReturnTextBox.Text;
            string actionTaken = actionTakenTextBox.Text;
            bool correctiveActionRequired = correctiveActionRequiredCheckBox.Checked;
            string ncNumber = ncNumberTextBox.Text;
            string notes = notesTextBox.Text;
            string results = "Supplier Name:\t" + supplierName + "\n\nReturn Address:\t" + returnAddress + "\n\nPrimary Contact Name:\t" + pcName + "\n\nPrimary Contact Phone:\t" + pcPhone + "\n\nPrimary Contact Fax:\t" + pcFax + "\n\nPrimary Contact Email\t" + pcEmail + "\n\nPart Number:\t" + partNumber + "\n\nPart Desc:\t" + partDescription + "\n\nPO Number:\t" + poNumber + "\n\nPO Line Number:\t" + poLineNumber + "\n\nSupplier Notification Date:\t" + supplierNotificationDate + "\n\nMaterial Return Authorizatized By:\t" + matReturnAuth + "\n\nReason Returned:\t" + reasonReturned + "\n\nAction Taken:\t" + actionTaken + "\n\nCorrective Action Required?\t" + correctiveActionRequired + "\n\nNC Number:\t" + ncNumber + "\n\nNotes:\t" + notes;

            MailMessage message = new MailMessage();

            message.From = new MailAddress("");

            //message.ReplyTo = new MailAddress(senderEmail);

            message.To.Add(new MailAddress(""));

            //message.To.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            //message.CC.Add(new MailAddress(""));

            message.Subject = "Return Parts Notice";

            message.Body = results;

            SmtpClient client = new SmtpClient();




Author Comment

ID: 18846079
I have the e-mail portion of it working again. With this code

OnClientClick="if (Page.IsValid == true) {window.print()}" />

the web form posts back and emails the information, but does not launch the print dialog.

With this code:

OnClientClick="window.print()" />

The print dialog opens, but there is no validation so the user could basically print off a blank form if they wanted to. I want to prevent that by using the Page.IsValid. Is it possible to call that from a client side script and still open the print dialog with the same button. I realize that I could use two buttons, but the users around here get confused easily.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 19

Accepted Solution

dakyd earned 125 total points
ID: 18846467
The trick is that Page is a .NET object, and it's instantiated on the server.  You can't access it directly from the javascript, because that exists on the client side.  

That said, though, if you enable client side validation on your validators, a bunch of javascript functions get created & included in an external js file.  Those functions can do the validation for you, since they are the client side equivalent of the pieces that make up Page.IsValid.

The easiest way to do this is to give all the validators that you want done on the client side a validation group.  Then, you can call a function called Page_ClientValidate(), and pass in the validation group.  For example:

OnClientClick="if (Page_ClientValidate('validGrp')) window.print();

I used a group name of "validGrp", but you can use anything you want.  If you already have different validator group names, or want to use validators from multiple groups, you'd want something like this:

OnClientClick="if (Page_ClientValidate('validGrp') && Page_ClientValidate('grp_name2') && Page_ClientValidate('grp_name3')) window.print();

You can add as many validator groups as you want, though I'd be surprised if you've got more than 3 validator groups.

If you're interested in seeing all the client-side functions, do a view source on your page, and look for the <script> tags that VS automatically adds in.  One of the WebResource.axd files will contain all of the validation functions (there is no specific name, it all depends on what else you've got on your page).  It's a pain to dig through, but the functions are all there.

Anyhow, hope that helps.

Author Comment

ID: 18846600
Thanks dakyd. That did exacly what I was looking for. I already had client side scripting enabled on my validation controls, but I never thought to group them. I had a felling that calling the Page class directly from the client side script wouldn't work out so well.

Expert Comment

ID: 21286104
What do I do If the page has userControls and the User Controls have there own validators.
In addition ,the page also have a javascript validation which validates the required fields which gets called as from a customvalidatior in the page.

If I use this solution,wont the validations be happening twice?


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question