繁体   English   中英

从日历事件中设置usercontrol属性

[英]set usercontrol property from event from calendar

在下面的代码中,调用属性更改的方法是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表单上的页面加载中,它工作正常,但是我正尝试通过日历selectionchange事件进行设置。

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属性设置为正确的值,但是当我继续运行应用程序时,它在标签上什么都没有显示。

仅当我尝试通过Calendar selectionchange事件设置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其是空
  3. 您单击日历,页面回发
  4. 页面再次加载->尚未设置BigText 空的
  5. 用户再次控制- > lblBigText被分配的值BigText其是空
  6. 触发Calendar_SelectionChanged事件->设置BigText
  7. 页面发送回给用户

请注意,在第6步中,在Page_LoadBigText属性实际上已设置之后触发Calendar_SelectionChanged 但是,标签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之前触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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