从日历中的事件设置用户控件属性

本文关键字:用户 控件 属性 设置 事件 日历 | 更新日期: 2023-09-27 18:33:43

在下面的代码中,调用属性更改的方法SetCaloriesBurnedStats(selectedDate),其他一切都正常工作。

我有一个简单的用户控件:

public string BigText { get; set; }
public string SmallText { get; set; }
public Color BigTextColor{get; set;}
public bool ArrowUp{get; set;}
public string SubBigText{get; set;}
protected void Page_Load(object sender, EventArgs e)
{
    lblBigText.Text = BigText;
    lblSmallText.Text = SmallText;
    lblBigText.ForeColor = BigTextColor;
    lblSubBigText.ForeColor = BigTextColor;
    lblSubBigText.Text = SubBigText;
    if (ArrowUp)
    {
        imgArrow.ImageUrl = "~/Images/trend-up-arrow.jpg";
    }
    else
    {
        imgArrow.ImageUrl = "~/Images/trend-down-arrow.jpg";
    }
}

它在我的 Web 表单上的页面加载时工作正常,但我正在尝试从日历选择更改事件中设置它。

protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
    DateTime selectedDate = Calendar.SelectedDate.Date;
    DateTime today = DateTime.Now.Date;
    if (selectedDate == today)
    {
        lblLogDayHeader.Text = "Today's Activity Log";
        lblSmallDate.Text = "Today";
    }
    else
    {
        lblLogDayHeader.Text = String.Concat("Activity Log For: ", Calendar.SelectedDate.ToShortDateString());
        lblSmallDate.Text = Calendar.SelectedDate.ToShortDateString();
    }
    SetActivityTable(selectedDate);
    SetCaloriesBurnedStats(selectedDate);
}
private void SetCaloriesBurnedStats(DateTime selectedDate)
{
    if (selectedDate.Date == DateTime.MinValue) { return; }
    using (var db = new DbConn())
    {
        var todaysCaloriesBurned =
            db.Activity.Where(c => c.Id == pId && SqlFunctions.DateDiff("DAY", c.DateOfEntry, selectedDate) == 0).Select(c => c.Calories).DefaultIfEmpty(0).Sum();
        Stat_CaloriesBurnedToday.BigText = todaysCaloriesBurned.ToString();
    }
}

在我的网络表单上,我添加了控件。

<uc1:Stat runat="server" BigTextColor="#07beb8" SubBigText="cals" SmallText="Calories burned today" ID="Stat_CaloriesBurnedToday" />

它始终返回 NULL,但在调试器中,我看到 BigText 属性设置为正确的值,但是当我继续运行应用程序时,它在标签上不显示任何内容。

仅当我尝试从日历选择更改事件设置 BigText 属性时,才会发生这种情况。

这是我的 Web 表单页面加载的代码,如果日历日期未更改,它不会调用事件。

protected void Page_Load(object sender, EventArgs e)
{
    DateTime calSelDate = Calendar.SelectedDate;
    DateTime selectedDate = DateTime.Now.Date;
    if (!Page.IsPostBack)
    {
        if (calSelDate == DateTime.MinValue)
        {
            SetActivityTable(selectedDate);
            SetCaloriesBurnedStats(selectedDate);
        } //Else set the table and stats in the Calender_SelectedDate event
    }
    ActivityChart = ReturnAllActivitiesForChart();
    SetAvgCaloriesBurnedDailyStats();
}

我在这里错过了什么?

从日历中的事件设置用户控件属性

我在这里错过了什么?

你错过了拼图中最重要的部分......页面生命周期。这是正在发生的事情

  1. 页面加载 -> BigText尚未设置。它是空的
  2. 用户控件加载 -> lblBigText分配了 BigText 的值,该值为 null
  3. 您单击日历,页面会回发
  4. 页面再次加载 -> BigText尚未设置。它是空的
  5. 再次为用户控件 -> lblBigText 分配了 BigText 的值,该值为 null
  6. 触发Calendar_SelectionChanged事件 -设置> BigText
  7. 页面将发送回给用户

请注意,在步骤 6 中,Calendar_SelectionChanged 是在Page_Load后触发的,并且实际设置了 BigText 属性。但是,标签lblBigText未分配此属性的值

溶液

打开 将 UserControl Page_Load"事件"中的所有初始化逻辑移动到在页面生命周期的后期和触发Render之前触发的事件。最安全的地方是OnPreRender如下所示...

    public string BigText { get; set; }
    public string SmallText { get; set; }
    public Color BigTextColor{get; set;}
    public bool ArrowUp{get; set;}
    public string SubBigText{get; set;}
    protected override void OnPreRender(object sender, EventArgs e)
    {
        base.OnPreRender(e);
        lblBigText.Text = BigText;
        lblSmallText.Text = SmallText;
        lblBigText.ForeColor = BigTextColor;
        lblSubBigText.ForeColor = BigTextColor;
        lblSubBigText.Text = SubBigText;
        if (ArrowUp)
        {
            imgArrow.ImageUrl = "~/Images/trend-up-arrow.jpg";
        }
        else
        {
            imgArrow.ImageUrl = "~/Images/trend-down-arrow.jpg";
        }
    }

永远记住,事件处理程序总是在OnLoadPage_Load之后但在OnPreRender之前触发。