Search code examples
asp.netvalidation-controls

ASP.Net web application - server side canceling Submit PostBackUrl if not(!) Page.IsValid


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?


Solution

  • 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); }
    }