Frage

I've created a new Web User Control. This control has a button. On the click event of this button I raise an event in this way:

public partial class SearchDate : UserControl, IPostBackEventHandler
{

    private static readonly object ObjEvent = new object();

    // delegates
    public delegate void VoidDelegate();

    // events
    [Category("Action")]
    [Description("Azionato quando si preme il cancella nel filtro data")]
    public event VoidDelegate CancelSearchDate
    {
        add
        {
            Events.AddHandler(ObjEvent, value);
        }

        remove
        {
            Events.RemoveHandler(ObjEvent, value);
        }

    }

    protected virtual void OnCancelSearchDate()
    {

        var cancelEvent = (EventHandler) Events[ObjEvent];

        if (cancelEvent != null)
        {
            cancelEvent(this, new EventArgs());
        }

    }

    protected void BtnSearchDateCancelClick(object sender, EventArgs e)
    {
        OnCancelSearchDate();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    public void RaisePostBackEvent(string eventArgument)
    {
        OnCancelSearchDate();
    }
}

I've created a delegate, an event and raise it on click of the button.

In another web form, where this control is hosted, i've added an handler to this event in this way:

    protected void Page_Load(object sender, EventArgs e)
    {


        if (Page.IsPostBack)
            return;


        InitDateFilter(SearchDate);

    }

    protected void InitDateFilter(SearchDate searchDateControl)
    {

        searchDateControl.CancelSearchDate += SearchDateControlCancelSearchDate;

    }

    void SearchDateControlCancelSearchDate()
    {
        // other stuff
        currUtente.FiltroData = null;
        SaveSession();
    }

When I click to the button, it try to evaluate the event but it's always null. I'm new in asp.net webforms and it seems to me strange that it not works well. I've tried to raise the event in the simpler way (without the 'add' and 'remove' on the event definition), just calling the event if it isn't null but the behavior is the same.

Thanks

War es hilfreich?

Lösung

try to just override the OnInit event in the Page and add the handler to your event there.

like this:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    InitDateFilter(SearchDate);
}

Let me know if it helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top