asp.net控件渲染和事件连线

时间:2016-05-21 15:01:43

标签: c# asp.net custom-controls

我正在构建一个自定义控件,但我遇到的问题是渲染是在PreRender上执行的,其中事件在生命周期中更快地连线,因此事件处理程序无法处理触发事件。我需要在事件可以在页面生命周期中处理的Init上连接事件。

我尝试在预渲染阶段之外初始化渲染对象,然后在该阶段添加对象但是失败,因为对象是通过foreach循环动态创建的,因此初始化对象不是创造得当。

因此,我的问题是,有一种方法可以在onInit阶段为预呈现阶段呈现的控件连接一个事件吗?

以下是控件的来源:

public class DayView : Panel, INamingContainer
{
    private DateTime _sd;
    public DateTime StartDate
    {
        get { return _sd; }
        set { _sd = value; }
    }

    public string LogoPath { [UsedImplicitly] private get; set; }

    public string InnerDCls { [UsedImplicitly] private get; set; }

    public string FsCCls { [UsedImplicitly] private get; set; }

    public string LegCCls { [UsedImplicitly] private get; set; }

    public string FieldClasses { [UsedImplicitly] private get; set; }


    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        writer.AddAttribute("ID", ClientID);
        base.AddAttributesToRender(writer);
    }

    public delegate void EPlannerEventHandler(object source, EventDataEventArgs args);

    public event EPlannerEventHandler FirstHalfAm;
    public event EPlannerEventHandler SecondHalfAm;
    public event EPlannerEventHandler FirstHalfPm;
    public event EPlannerEventHandler SecondHalfPm;

    protected override void OnInit(EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }

    private void DayPlanner()
    {
        var daysInWeek = new List<DateTime>();
        for (var i = 0; i < 7; i++)
        {
            daysInWeek.Add(StartDate.AddDays(i));
        }
        var containerTbl = new Table { CssClass = "ct_cls" };
        var containerRow = new TableRow();
        foreach (var day in daysInWeek)
        {
            var containerCell = new TableCell();
            containerCell.CssClass = "cc_cls";
            var weekTbl = new Table();
            weekTbl.Style.Add("width", "100%");
            weekTbl.CssClass = "tblDplan_cls";
            var weekdayRow = new TableRow();
            var weekdayCell = new TableCell();
            weekdayCell.ColumnSpan = 2;
            weekdayCell.CssClass = "dayPlan_cls";
            try
            {
                weekdayCell.Controls.Add(new LiteralControl(day.ToString("ddd, MMMM dd, yyyy")));
            }

            catch (HttpException)
            {
                Debug.Assert(weekdayCell.HasControls());
            }

            catch (ArgumentNullException argumentNullException)
            {
                Console.WriteLine(new ArgumentNullException(argumentNullException.Message));
            }
            weekdayRow.Cells.Add(weekdayCell);
            weekTbl.Rows.Add(weekdayRow);


            foreach (var hours in DateExtensions.HoursInDay())
            {
                var hourRow = new TableRow();
                var hourCell = new TableCell();
                hourCell.CssClass = "hour_cls";
                hourCell.RowSpan = 2;
                try
                {
                    hourCell.Controls.Add(new LiteralControl(hours + "am"));
                }
                catch (HttpException httpException)
                {
                    Console.WriteLine(httpException.Message);
                }
                hourRow.Cells.Add(hourCell);

                var firstHalfAm = new TimeLink
                {
                    TlCssClass = "hour2_cls",
                    Identifier = day.Day,
                    WeekDate = day,
                    Date = day,
                    Time = hours + ":00" + "am"
                };
                firstHalfAm.DayLink += FirstHalfAm_DayLink;
                hourRow.Cells.Add(firstHalfAm);
                weekTbl.Rows.Add(hourRow);

                var hourhalfRow = new TableRow();
                var secondHalfAm = new TimeLink
                {
                    TlCssClass = "hour2_cls",
                    Identifier = day.Day,
                    WeekDate = day,
                    Date = day,
                    DisplayLabel = "30",
                    Time = hours + ":30" + "am"
                };
                secondHalfAm.DayLink += SecondHalfAm_DayLink;
                hourhalfRow.Cells.Add(secondHalfAm);
                weekTbl.Rows.Add(hourhalfRow);
            }
            foreach (var hourpm in DateExtensions.HoursInDay())
            {

                var hourRow = new TableRow();
                var hourCell = new TableCell();
                hourCell.RowSpan = 2;
                hourCell.CssClass = "hour_cls";
                try
                {
                    hourCell.Controls.Add(new LiteralControl(hourpm + "pm"));
                }
                catch (HttpException httpException)
                {
                    Console.WriteLine(httpException.Message);
                }
                hourRow.Cells.Add(hourCell);

                var firstHalfPm = new TimeLink
                {
                    TlCssClass = "hour2_cls",
                    Identifier = day.Day,
                    WeekDate = day,
                    Date = day,
                    Time = hourpm + ":00" + "pm"
                };
                firstHalfPm.DayLink += FirstHalfPm_DayLink;
                hourRow.Cells.Add(firstHalfPm);
                weekTbl.Rows.Add(hourRow);

                var hourhalfRow = new TableRow();
                var secondHalfPm = new TimeLink
                {
                    TlCssClass = "hour2_cls",
                    Identifier = day.Day,
                    WeekDate = day,
                    Date = day,
                    DisplayLabel = "30",
                    Time = hourpm + ":30" + "pm"
                };
                secondHalfPm.DayLink += SecondHalfPm_DayLink;
                hourhalfRow.Cells.Add(secondHalfPm);
                weekTbl.Rows.Add(hourhalfRow);
            }
            try
            {
                containerCell.Controls.Add(weekTbl);
            }
            catch (HttpException httpException)
            {
                Console.WriteLine(httpException.Message);
            }
            containerRow.Cells.Add(containerCell);
        }
        daysInWeek.Clear();
        containerTbl.Rows.Add(containerRow);
        try
        {
            Controls.Add(containerTbl);
        }
        catch (HttpException httpException)
        {
            Console.WriteLine(httpException.Message);
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        DayPlanner();
        base.OnPreRender(e);
    }

    private void SecondHalfPm_DayLink(object source, EventDataEventArgs args) => SecondHalfPm?.Invoke(source, args);

    private void FirstHalfPm_DayLink(object source, EventDataEventArgs args) => FirstHalfPm?.Invoke(source, args);

    private void SecondHalfAm_DayLink(object source, EventDataEventArgs args) => SecondHalfAm?.Invoke(source, args);

    private void FirstHalfAm_DayLink(object source, EventDataEventArgs args) => FirstHalfAm?.Invoke(source, args);

    protected override object SaveControlState()
    {
        return StartDate;
    }

    protected override void LoadControlState(object state)
    {
        if (state != null)
        {
            StartDate = (DateTime)state;
        }
    }

}

DayPlanner()是一个只是渲染过程容器的方法。在整个过程中移动更容易;

1 个答案:

答案 0 :(得分:0)

由于您正在构建服务器端表和其他控件,因此您必须在PreRender之前构建此UI;通常情况发生在Init ...但是MS在构建CompositeControl base class时考虑到了这一点。这可能是一个更好的选择。