1

I have a web application, and the submit uses the PostBackUrl to display data if the form is valid.

I need to use different (server side) validation groups, depending on what radiobutton is selected. That is, manually call

Page.Validate('ValidationGroupA').

The validation itself is working fine, in terms of Page.IsValid correctly assigned true or false.

However, if the page is not valid, the page redirects to the page specified by PostBackUrl on the submit button, where the page is found not to be valid, and the client redirected back with:

if (!PreviousPage.IsValid){Response.Redirect("DataEntryPage.aspx");})

However, by getting to here and back again, all form data is lost, and the relevant validation controls and summaries are not displayed.

Is there any equivalent of JavaScripts evt.preventDefault() or some other way, once it is detected validation has failed after a manual call to Page.Validate to post the form back with appropriate validation errors displayed?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Brent
  • 4,611
  • 4
  • 38
  • 55

2 Answers2

0

Use ASP.Net Client Side Validation.

jQuery Code (call this on onclick of submit button):

  $("#SubmitButtonID").click(function (e) {
      Page_ClientValidate('ValidationGroupA');
      if (!Page_IsValid)
      {
         e.preventDefault();
      }
  });
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
  • Thanks for your answer. I am actually currently using client side javascript extensively similar to your suggestion. However, this validation needs to occur server side, as it needs to lookup a database. I could write some ajax to do the validation, but these validations are final fail-safes for for edge cases, and thus it does not seem worth it. – Brent Oct 31 '12 at 06:54
0

I ended up removing the PostBackUrl directive, and using an onclick event for the submit button

protected void Submit_Click(object sender, EventArgs e)
{
   if (chartTypeRadio.Text != "bolus") { Page.Validate("age"); }
   if (Page.IsValid) { Server.Transfer("~/PatientSpecificDrugChart.aspx", true); }
}
Brent
  • 4,611
  • 4
  • 38
  • 55