简体   繁体   中英

Events from UserControl in Gridview are not being fired

I have a gridview that has a usercontrol in a template column on each row.

During the RowDataBound event I subscribe to the event as follows

DateTimePicker dtp = (DateTimePicker)e.Row.FindControl("DateTimePickerPromoRunDate");
dtp.OnDateChanged +=new CustomEventHandler(dtp_OnDateChanged);

And then in the control I have the following

public class CustomEventArgs : EventArgs
{
    public CustomEventArgs(DateTime dateSelected)
    {
        DateSelected = dateSelected;
    }
    public DateTime DateSelected { get; set; }
}

public delegate void CustomEventHandler(object sender, CustomEventArgs e);

public partial class DateTimePicker : System.Web.UI.UserControl
{
    public event CustomEventHandler OnDateChanged;
    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        e.DateSelected = Convert.ToDateTime(TextBoxDate.Text);
        if (OnDateChanged != null)
            OnDateChanged(this, e);
    }
 [blah blah blah]
 OnCustomEvent(new CustomEventArgs(Calendar1.SelectedDate));

The problem is that when I call the OnCustomEvent method the OnDateChanged event is null as if I haven't subscribed to it.

A similar problem exists where if I set the properties of the usercontrol on databound when I attempt to use it those properties are null... can someone point out what I am doing wrong here?

You may have to subscribe your ondatechanged on each post back. otherwise the page doesn't remember it.

I managed to get around my problem by simply putting the event call in the metadata side of the page. It's not ideal but it's firing now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM