在下面的代码中,调用属性更改的方法是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";
}
}
它在我的网络表单上的页面加载时工作正常,但我正在尝试从日历选择更改事件中设置它。
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属性时才会发生这种情况。
以下是我的网页表单页面加载的代码,如果日历日期未更改,则不会调用该事件。
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();
}
我在这里缺少什么?
答案 0 :(得分:1)
我在这里缺少什么?
你错过了拼图中最重要的部分......页面生命周期。这里发生了什么
BigText
尚未设定。它是空的lblBigText
的值为BigText
,为空BigText
尚未设定。它是空的lblBigText
的值为BigText
,为空Calendar_SelectionChanged
事件被触发 - &gt; BigText
已设置请注意,在步骤6中,在Calendar_SelectionChanged
之后触发Page_Load
并且实际设置了BigText
属性。但是,标签lblBigText
未分配此属性的值
打开移动UserControl&#39; s Page_Load
&#34; event&#34;中的所有初始化逻辑。在页面生命周期的后期和触发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";
}
}
始终记住事件处理程序始终在OnLoad
和Page_Load
之后但在OnPreRender
之前触发。