单击链接按钮时更新用户控件公共属性

发布于 2024-10-16 02:39:54 字数 1474 浏览 2 评论 0原文

我创建了一个非常简单的日历用户控件,其中包含一个公共属性,用于定义要显示的月份/年份。我的日历控件放置在另一个 ascx 控件(本质上是包装控件)内,其中包括用于一次向前和向后切换日历控件一个月的 LinkBut​​ton 控件。如果我在设计时在包装控件中显式定义日历控件的 SelectedDateTime 属性,则日历控件将显示正确的月份/年份。但是,当我尝试在运行时使用上一个和下一个 LinkBut​​ton 控件上的 Click 事件设置属性值时,日历控件永远不会拾取我尝试分配给它的新 SelectedDateTime 值。

当我在调试模式下运行代码时,LinkBut​​ton 的 Click 事件总是最后发生 - 在日历控件的 SelectedDateTime 属性的值已设置为默认值并呈现之后。我缺少什么?

日历控件像任何旧控件一样放置在包装器控件中:

<wsba:CalendarBase ID="CalendarControl" runat="server" />

我创建了一个 LinkBut​​ton 控件来选择“下个月”,及其相应的 Click 事件处理程序,如下所示:

    protected void NextMonthLink_Click(object sender, EventArgs e)
    {
        CalendarControl.SelectedDateTime = CalendarControl.SelectedDateTime.AddMonths(1);            
    }

这是日历控件本身的核心:

    public partial class CalendarBase : System.Web.UI.UserControl
{
    public DateTime SelectedDateTime { get; set; }
    public List<Item> SelectedCalendars { get; set; }

    public CalendarBase()
    {
        // default SelectedDateTime to Now if no value was set
        if (SelectedDateTime.Year.Equals(1)) { SelectedDateTime = DateTime.Now; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // grab the month and year from the selectedDateTime
        int selectedMonth = SelectedDateTime.Month;
        int selectedYear = SelectedDateTime.Year;
        ... Do the rest of the stuff to render my control ...
    }

I have created a very simple calendar user control that contains a public property which is used to define which month/year to display. My calendar control is placed inside another ascx control (the wrapper control essentially), which includes LinkButton controls used to toggle the calendar control forward and backward one month at a time. If I explicitly define the SelectedDateTime property of the calendar control in the wrapper control at design time, the proper month/year are displayed by the calendar control. However, when I try to set the property value at run time using a Click event on the previous and next LinkButton controls, the calendar control never picks up the new SelectedDateTime value I try to assign to it.

When I run the code in debug mode, the Click event of the LinkButton always happens last - after the value of the SelectedDateTime property of the calendar control has already been set to default and rendered. What am I missing?

The calendar control is placed in the wrapper control just like any old control:

<wsba:CalendarBase ID="CalendarControl" runat="server" />

I created a LinkButton control to select "Next Month", and its corresponding Click event handler like so:

    protected void NextMonthLink_Click(object sender, EventArgs e)
    {
        CalendarControl.SelectedDateTime = CalendarControl.SelectedDateTime.AddMonths(1);            
    }

This is the meat of the calendar control itself:

    public partial class CalendarBase : System.Web.UI.UserControl
{
    public DateTime SelectedDateTime { get; set; }
    public List<Item> SelectedCalendars { get; set; }

    public CalendarBase()
    {
        // default SelectedDateTime to Now if no value was set
        if (SelectedDateTime.Year.Equals(1)) { SelectedDateTime = DateTime.Now; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // grab the month and year from the selectedDateTime
        int selectedMonth = SelectedDateTime.Month;
        int selectedYear = SelectedDateTime.Year;
        ... Do the rest of the stuff to render my control ...
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-10-23 02:39:54

我可能在这里遗漏了一些东西,但是如果您的日历用户控件依赖公共属性来更改其输出。您应该在 Page_Load 事件之后的事件中呈现该控件。这将确保在对其属性进行任何设置之后进行渲染。

您可以/应该将日历控制代码从 Page_Load 移动到 Page_PreRender 事件。这样,它总是会在其他服务器事件触发和属性更改后呈现。

MSDN 页面生命周期页面

I'm likely missing something here, but if your Calendar User Control relies of Public Properties to change its output. You should render the control in an event AFTER the Page_Load event. This will ensure that the rendering occurs after any setting of its properties.

You could/should move your Calendar control code from Page_Load to the Page_PreRender event. That way it will always render after other server events have fired and properties changed.

MSDN Page Lifecycle page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文