Solved

cross page posting and validation problems

Posted on 2006-06-23
4
1,195 Views
Last Modified: 2008-02-01
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
        }
      }
0
Comment
Question by:mbosico
  • 2
4 Comments
 
LVL 6

Expert Comment

by:e1v
Comment Utility
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();
        }

0
 
LVL 4

Expert Comment

by:gregg1ep00
Comment Utility
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?
0
 

Author Comment

by:mbosico
Comment Utility
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)
0
 
LVL 4

Accepted Solution

by:
gregg1ep00 earned 250 total points
Comment Utility
If you want it to be validated on the client side, change the CustomValidator to a CompareValidator, and in your code-behind (in Page_Load), set the CompareValidator's "ValueToCompare" property to the formshield's text property.

This will pose a security risk, however, since your formshield's text will be visible in plain text when you view the source of the rendered page (look in the javascript functions in that page).
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now