mrcoulson
mrcoulson

Reputation: 1403

Can I call a method from another class in an ASPX page?

I have some logic in a class called CustomValidation that I'm currently calling in my C# code:

protected void EmailValidator(object source, ServerValidateEventArgs args)
{
    string strInput = args.Value.Trim();
    CustomValidation v = new CustomValidation();
    args.IsValid = v.ValidateEmail(strInput);
}

I would rather call such methods from my ASPX page to simplify things, like this:

<asp:CustomValidator OnClick="ValidateEmail" />

However, since ValidateEmail is not in the same class as the page:

'ASP.editusers_aspx' does not contain a definition for 'ValidateEmail' and no extension method 'ValidateEmail' accepting a first argument of type 'ASP.editusers_aspx' could be found (are you missing a using directive or an assembly reference?)

Is there a way to call methods from the CustomValidation class in my ASPX code? I've tried the following to no avail:

<%@ Import Namespace="ThisProject.CustomValidation" %>

OnClick="<%= CustomValidation.ValidateEmail()" %>

Here is my usage in context:

<asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("UserEmail") %>' MaxLength="30" />
<asp:CustomValidator ID="vldEmail" runat="server" ControlToValidate="txtEmail" ValidateEmptyText="true" OnServerValidate='<%# RetailCrime.CustomValidation.ValidateEmail((string)(txtEmail.Text)) %>' ErrorMessage='<br /><label class="invisible noplaceholder">&nbsp;</label>Please enter a valid email.' Display="Dynamic" CssClass="validatormessage" />

Upvotes: 3

Views: 3162

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156918

You can achieve this by making a static delegate.

public class CustomValidator
{
    public static void EmailValidator(object source, ServerValidateEventArgs args)
    {
        string strInput = args.Value.Trim();
        CustomValidation v = new CustomValidation();
        args.IsValid = v.ValidateEmail(strInput);
    }
}

<asp:CustomValidator OnClick="CustomValidator.EmailValidator" />

Upvotes: 4

Juan Ayala
Juan Ayala

Reputation: 3518

It sounds like you have a validator method that you want to access from at least 2 or more pages. Remember the single responsibility principle.

While the static approach works, you can statically access the validation method that is defined in the edit users page class. But this email validation is really quite generic and could be used for more than just editing users. So if you look at your original error "... does not contain a definition for 'ValidateEmail' and no extension method ...", you could create an extension method.

To extend a class you'll need a common interface to apply to any class you want to be extended. For example:

public interface IMyValidations { }

and create the extension class

public static class MyValidations
{
    public static void ValidateEmail(this IMyValidations target, string input)
    {
        // magic happens here
    }
}

and create your concrete classes

public class MyFirstClass : IMyValidations
{
}

public class MySecondClass : IMyValidations
{
}

and wherever or however you create an instance of your concrete classes, you can access that method as an instance method

 MyFirstClass one = new MyFirstClass();
 one.ValidateEmail("[email protected]");
 MySecondClass two = new MySecondClass();
 two.ValidateEmail("[email protected]");

This example is far from complete, it was thrown together to show how you can use an extension method to take that email validation out of that class. Obviously why would you pass in the email when you already have an instance of the target?

Any my final suggestion, email validation, and validation in general has been done time and time again. No need to re-invent the wheel. I'm sure there are a bazillion frameworks out there, but my favorite is FluentValidations. NuGet makes it ridiculously easy to set up.

Upvotes: 0

Related Questions