Link to home
Start Free TrialLog in
Avatar of mbosico
mbosico

asked on

cross page posting and validation problems

I have a page that uses a customvalidator, so it does some server-side validation and I also put a PostBackUrl on the submit buttom. problem occurs that when i click the buttom, regardless if the validation failed or succeeded, it posts to the page. now i've read that i can just do PreviousPage.IsValid in the form_load event of the posted page to see if its valid, but how do I return to the previous page with the information intact...

///// REGISTER.ASPX //////

<asp:RequiredFieldValidator ID="formShieldRequiredVd" runat="server" ControlToValidate="formShieldTx"
        ErrorMessage="Anti-Hacker Text Required" ForeColor="InactiveCaption">*</asp:RequiredFieldValidator>&nbsp;<asp:CustomValidator
            ID="formShieldDontMatchVd" runat="server"
            ControlToValidate="formShieldTx" ErrorMessage="Anti-Hacker text does not match"
            ForeColor="InactiveCaption" OnServerValidate="formShieldDontMatchVd_ServerValidate" Enabled="true">*</asp:CustomValidator>
<asp:Button ID="okBt" runat="server" Text="Register" PostBackUrl="~/emailInUse.aspx" />

protected void formShieldDontMatchVd_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            args.IsValid = (formShieldTx.Text.CompareTo(
                this.formShield.Text) == 0);
        }
        catch (Exception)
        {
            args.IsValid = false;
        }
    }

////// emailInUse.aspx //////
protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null && !PreviousPage.IsValid)
        {
              // need to redirect back to the previous page with data intact somehow and have the validation error displayed
        }
      }
Avatar of e1v
e1v

This is what I did in a situation like yours, I only had one textbox (TextBoxEmail) on the previous page (Register.aspx) that had to be restored.

in Page_Load of the destination page:

if (PreviousPage != null && !PreviousPage.IsValid)
{
     Session["TextBoxEmail"] = ((TextBox)PreviousPage.FindControl("TextBoxEmail")).Text;
     Response.Redirect("~/Register.aspx", false);
}

then in page_Load of Register.aspx:
   if (Session["TextBoxEmail"] != null)
        {
            TextBoxEmail.Text = (string)Session["TextBoxEmail"];
            Session.Remove("TextBoxEmail");
            Validate();
        }

Can you validate on the current page, and then if validation succeeds, then do a Response.Redirect() or a Server.Transfer() to your new page, instead of validating on a different page?
Avatar of mbosico

ASKER

gregg1ep00:

I'd like it to be validated on the client side, but I don't know how because its a custom validation, I dont know how to get to the controls fields to check against what they type in.

formShieldTx.Text.CompareTo(this.formShield.Text) == 0)
ASKER CERTIFIED SOLUTION
Avatar of gregg1ep00
gregg1ep00

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial