ComfortablyNumb
ComfortablyNumb

Reputation: 1456

Call an EventHandler from another Method

How can I call the following method from another method on same code behind page?

protected void CustomValidatorDelLN_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        bool is_valid = txtDeliveryLastName.Text != "";
        txtDeliveryLastName.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
        args.IsValid = is_valid;
    }

I don't know how to handle the (object sender, ServerValidateEventArgs args) bit. I call CustomValidatorDelLN_ServerValidate(); What do I put inside the brackets?

Upvotes: 4

Views: 2773

Answers (7)

Julius
Julius

Reputation: 11

for the (object sender, ServerValidateEventArgs args) bit put this: (this, new EventArgs())

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245489

Since you're not directly referencing the sender, and you're not properly using the ServerValidateEventArgs, you can shortcut things a bit:

var args = new ServerValidateEventArgs(String.Empty, false);
CustomValidatorDelLN_ServerValidate(null, args);

I wouldn't do that though. I would suggest a refactor. Calling an Event Handler from other code really doesn't make sense. You could easily pull out the validation logic and put it in a separate method. You could then use that new method from both spots in your code:

// You can call this method from both places
protected bool ValidateLastName()
{
    bool isValid = !String.IsNullOrWhiteSpace(txtDeliveryLastName.Text);
    txtDeliveryLastName.BackColor = isValid ? Color.White : Color.LightPink;
    return isValid;
}

// This would be the modified Event Handler
protected void CustomValidatorDelLN_ServerValidate(object sender,
    ServerValidateEventArgs args)
{
    args.IsValid = ValidateLastName();
}

Upvotes: 6

Joel Etherton
Joel Etherton

Reputation: 37543

The method you're referring to is an event and it needs to be wired up to your validator either through the html or in a page event. Example:

<asp:CustomValidator OnServerValidate="CustomValidatorDelLN_ServerValidate" />

or

protected void Page_Load(object sender, EventArgs e)
{
    CustomValidatorDelLN.ServerValidate += CustomValidatorDelLN_ServerValidate;
}

Reference: http://msdn.microsoft.com/en-us/library/system.web.ui.mobilecontrols.customvalidator.servervalidate.aspx

Upvotes: 0

Jeremy McGee
Jeremy McGee

Reputation: 25210

Try breaking out another method:

private bool ValidateDeliveryLastName()
    {
        bool is_valid = txtDeliveryLastName.Text != "";
        txtDeliveryLastName.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
        return is_valid;
    }

then use the call

protected void CustomValidatorDelLN_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = ValidateDeliveryLastName();
    }

and likewise in your other method, whatever that might be.

Upvotes: 1

CodingGorilla
CodingGorilla

Reputation: 19872

Since your code doesn't actually use the sender parameter you can simply pass in a null. As for the ServerValidateEventArgs you can just new it up, there's no magic.

CustomValidatorDelLN_ServerValidate(null, new ServerValidateEventArgs(String.Empty, false));

Upvotes: 0

archil
archil

Reputation: 39501

Extract that validation logic to another method

public bool CheckValidity()
{
        bool is_valid = txtDeliveryLastName.Text != "";
        txtDeliveryLastName.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
        return is_valid;
}

And use it

protected void CustomValidatorDelLN_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = CheckValidity();
    }

Now call CheckValidity() from anywhere

Upvotes: 3

2GDev
2GDev

Reputation: 2466

Something Like this can work...

    protected void CustomValidatorDelLN_ServerValidate(object sender, ServerValidateEventArgs args)
    {

        args.IsValid = isValid();
    }


protected bool isValid()
{

    bool is_valid = txtDeliveryLastName.Text != "";
        txtDeliveryLastName.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
    return is_valid;
}

Upvotes: 1

Related Questions