odie532
asked on
Validating form fields before printing with client side scripts
Hello,
I am creating a webform using Visual Studio 2005 with asp.net 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.prin t()" /> (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()}" />
I am creating a webform using Visual Studio 2005 with asp.net 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
This is what I have come up with to do the validation, but I can't get it to work properly:
OnClick="printButton_Click
ASKER
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.Tex t;
string poNumber = poNumberTextBox.Text;
string poLineNumber = poLineNumberTextBox.Text;
string supplierNotificationDate = supplierNotificationTextBo x.Text;
string matReturnAuth = returnMatAuthTextBox.Text;
string reasonReturned = reasonReturnTextBox.Text;
string actionTaken = actionTakenTextBox.Text;
bool correctiveActionRequired = correctiveActionRequiredCh eckBox.Che cked;
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("WebForms@prec ision-aero space.com" );
//message.ReplyTo = new MailAddress(senderEmail);
message.To.Add(new MailAddress("rmarkley@prec ision-aero space.com" ));
//message.To.Add(new MailAddress("kdorr@precisi on-aerospa ce.com"));
//message.CC.Add(new MailAddress("rheyboer@prec ision-aero space.com" ));
//message.CC.Add(new MailAddress("kwozniak@prec ision-aero space.com" ));
//message.CC.Add(new MailAddress("dklompf@preci sion-aeros pace.com") );
//message.CC.Add(new MailAddress("jwaller@preci sion-aeros pace.com") );
//message.CC.Add(new MailAddress("jcase@precisi on-aerospa ce.com"));
//message.CC.Add(new MailAddress("mkempf@precis ion-aerosp ace.com")) ;
message.Subject = "Return Parts Notice";
message.Body = results;
SmtpClient client = new SmtpClient();
client.Send(message);
//Response.Redirect("confi rmationPag e.htm");
}
}
}
OnClick="printButton_Click
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.Tex
string poNumber = poNumberTextBox.Text;
string poLineNumber = poLineNumberTextBox.Text;
string supplierNotificationDate = supplierNotificationTextBo
string matReturnAuth = returnMatAuthTextBox.Text;
string reasonReturned = reasonReturnTextBox.Text;
string actionTaken = actionTakenTextBox.Text;
bool correctiveActionRequired = correctiveActionRequiredCh
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("WebForms@prec
//message.ReplyTo = new MailAddress(senderEmail);
message.To.Add(new MailAddress("rmarkley@prec
//message.To.Add(new MailAddress("kdorr@precisi
//message.CC.Add(new MailAddress("rheyboer@prec
//message.CC.Add(new MailAddress("kwozniak@prec
//message.CC.Add(new MailAddress("dklompf@preci
//message.CC.Add(new MailAddress("jwaller@preci
//message.CC.Add(new MailAddress("jcase@precisi
//message.CC.Add(new MailAddress("mkempf@precis
message.Subject = "Return Parts Notice";
message.Body = results;
SmtpClient client = new SmtpClient();
client.Send(message);
//Response.Redirect("confi
}
}
}
ASKER
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.prin t()" />
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.
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.prin
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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?
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?
if (Page.IsValid == true)