简体   繁体   English

从日历事件中设置usercontrol属性

[英]set usercontrol property from event from calendar

On the following code the method that's calling the property change is SetCaloriesBurnedStats(selectedDate) , everything else is working correctly. 在下面的代码中,调用属性更改的方法是SetCaloriesBurnedStats(selectedDate) ,其他所有东西都正常运行。

I have a simple user control: 我有一个简单的用户控件:

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";
    }
}

It works fine on page load on my web forms, but I'm trying to set it from a calendar selectionchange event. 在我的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();
    }
}

On my webform I have the control added. 在我的网络表单上,我添加了控件。

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

It always returns NULL, but in the debugger I'm watching the BigText property being set to the correct value, but when I continue running the app it shows nothing on the label. 它总是返回NULL,但是在调试器中,我正在看着BigText属性设置为正确的值,但是当我继续运行应用程序时,它在标签上什么都没有显示。

This only happens when I'm trying to set the BigText property from the Calendar selectionchange event. 仅当我尝试通过Calendar selectionchange事件设置BigText属性时,才会发生这种情况。

Here is the code for my web forms page load which doesn't call the event if the calendar date isn't changed. 这是我的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();
}

What am I missing here? 我在这里想念什么?

What am I missing here? 我在这里想念什么?

You are missing the most important part of the puzzle...the Page Life Cycle. 您错过了拼图中最重要的部分……页面生命周期。 Here's what's happening 这是发生了什么

  1. The page loads -> BigText hasn't been set. 页面加载->未设置BigText It's null 空的
  2. The user control loads -> lblBigText is assigned the value of BigText which is null 用户控制负载- > lblBigText被分配的值BigText其是空
  3. You click on the Calendar and the page posts back 您单击日历,页面回发
  4. The page loads again -> BigText hasn't been set. 页面再次加载->尚未设置BigText It's null 空的
  5. The user control again -> lblBigText is assigned the value of BigText which is null 用户再次控制- > lblBigText被分配的值BigText其是空
  6. Calendar_SelectionChanged event is triggered -> BigText is set 触发Calendar_SelectionChanged事件->设置BigText
  7. The page is sent back to the user 页面发送回给用户

Notice in step 6 that Calendar_SelectionChanged is triggered after Page_Load and the BigText property is actually set. 请注意,在第6步中,在Page_LoadBigText属性实际上已设置之后触发Calendar_SelectionChanged However, label lblBigText is not assigned the value of this property 但是,标签lblBigText没有分配此属性的值

Solution

Open move all the initialization logic inside your UserControl's Page_Load "event" to an event that is triggered later in the page life cycle and before Render is triggered. 打开,将UserControl的Page_Load “事件”中的所有初始化逻辑移到一个事件,该事件将在页面生命周期的稍后且触发Render之前触发。 The safest place would be OnPreRender as shown below... 最安全的地方是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";
        }
    }

Always remember that event handlers are always triggered after OnLoad and Page_Load but before OnPreRender . 始终记住,事件处理程序总是在OnLoadPage_Load但在OnPreRender之前触发。

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

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