0

I have the following in my .aspx file:

    <asp:CustomValidator
        ID="JobIDCustomFieldValidator"
        runat="server"
        ControlToValidate="JobID"
        OnServerValidate="jobIDCustom_ServerValidate"
        EnableClientScript="false"
        SetFocusOnError="true"
        Display="Dynamic"
        ErrorMessage="! - Not Found"
        CssClass="validationError">
   </asp:CustomValidator>
   <br />        
   <asp:TextBox ID="JobID" runat="server"></asp:TextBox>
   <asp:Button runat="server" ID="ProcessButton"  Text="Process" onclick="ProcessButton_Click" />

I have the following in my code behind file:

    protected void ProcessButton_Click(object sender, EventArgs e)
    {
        Response.Write("I am in here");
    }

    protected void jobIDCustom_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        // Impersonate a user for shared folder access.
        using (UserImpersonation user = new UserImpersonation(properties.ShareUser, properties.Domain, properties.SharePassword))
        {
            e.IsValid = false;

            // Check the user credentials.
            if (user.ImpersonateValidUser())
            {
               e.IsValid = File.Exists(@"\\\\" + properties.RemoteServer + "\\" + properties.Share + "\\" + JobID.Text + ".dat");
            }
        }
    }

I want the custom validator to be checked first and if it is false stop and do not run any of the code in the ProcessButton_Click() method. Is this possible? If not is there an alternative way I could set this up?

As far as I know I can't use client side validation with javascript to do the impersonating and file access.

Any help would be greatly appreciated.

Baxter
  • 5,633
  • 24
  • 69
  • 105
  • 1
    It should do that automatically with CausesValidation="true" on your button, right? – Cj S. Jul 24 '13 at 17:50
  • @CjS. I added CausesValidation="true" to my button but it is still running the code in the ProcessButton_Click() when the custom validation fails and prints its error message. – Baxter Jul 24 '13 at 18:08
  • 1
    How about adding Page.Validate(); and testing Page.IsValid in your button click handler? – Cj S. Jul 24 '13 at 18:10
  • @CjS. I added the following to my button click handler: Page.Validate(); Response.Write(Page.IsValid); Response.Write("I am in here"); When I click the button it prints this: FalseI am in here – Baxter Jul 24 '13 at 18:15
  • 1
    Great! So your validation is working. Not instead of writing Page.IsValid, use it to decide if you want to do any additional work in your event handler... If (Page.IsValid) {Response.Write("I am in here");} – Cj S. Jul 24 '13 at 18:17
  • @CjS. Perfect! That should work nicely. Thank you for your help. – Baxter Jul 24 '13 at 18:18

2 Answers2

2

To summarize check to see if the page is valid in the button click handler.

protected void ProcessButton_Click(object sender, EventArgs e)
{
        if (Page.IsValid)
        {
            //do button stuff
        }   
}
Baxter
  • 5,633
  • 24
  • 69
  • 105
0

"Validation controls test user input, set an error state, and produce error messages. They do not change the flow of page processing—for example, they do not bypass your code if they detect a user input error. Instead, you test the state of the controls in your code before performing application-specific logic. If you detect an error, you prevent your own code from running; the page continues to process and is returned to the user with error messages."

From MSDN

http://msdn.microsoft.com/en-us/library/dh9ad08f(v=vs.90).aspx

Lynn
  • 81
  • 1
  • 3