【问题标题】:Setting date picker value to specific page将日期选择器值设置为特定页面
【发布时间】:2015-08-16 04:05:39
【问题描述】:

我有一个通用的日期选择器,这个日期选择器在近 10 个页面上很常见,因为默认日期选择器日期设置为当前日期,当我选择该链接时,我有一个带有链接的页面(10 页中的 1 页) ,它包含一些日期(未来或过去),所以我希望链接选择的日期显示在日期选择器控件上,目前我能够做到这一点,我面临的问题是,相同的更改日期反映到其他 9 页我不需要,我应该能够看到它们的默认日期,即当前日期。

代码:

特定页面的方法我将其设置为特定日期

protected void QuickListDateNavigate(object sender, CommandEventArgs e)
    {
        DateTime newDate = DateTime.Parse((string)e.CommandArgument);
        this.Parent.SessionObj.ViewDate = newDate;
    }

这是将上述选定日期设置为链接选定日期以及其他 9 个页面的位置

日期选择器.ascx.cs

 protected override void OnPreRender(EventArgs e)
    {

        if (this.m_showDateText)
        {
            if (datepicker.Value == "")
            {
                PickerDate = ViewDate;
            }
            else
            {
              //  ViewDate = DateTime.Parse(datepicker.Value);
                ViewDate = this.Parent.SessionObj.ViewDate;
                PickerDate = ViewDate;
            }
        }
        base.OnPreRender(e);
    }

之前它是设置当前日期的注释行,添加了下一个链接“ViewDate = this.Parent.SessionObj.ViewDate;”正如我预期的那样改变了日期。

 public DateTime ViewDate
    {
        get
        {
            return Parent.SessionObj.ViewDate;
        }
        set
        {
            Parent.SessionObj.ViewDate = value;
        }
    }

 protected DateTime PickerDate
    {
        get
        {
            DateTime newDate = ViewDate;//Use current ViewDate if Value in textbox is not valid.
            string pickerValue = this.Parent.SessionObj.ViewDate.ToString();
            try
            {
                newDate = DateTime.Parse(pickerValue);
            }
            catch
            {
                //Date was not a valid format fill in with the ViewDate
                SetPickerDateToViewDate();
            }
            return newDate;
        }
        set
        {
            this.datepicker.Value = value.ToString(this.m_dateFormatString);
        }
    }

 protected void SetPickerDateToViewDate()
    {
        PickerDate = ViewDate;
    }

因此,一旦将日期更改为我的要求,它就不会设置回其他页面的当前日期,有什么办法可以将更改的日期用于特定页面和其他页面的默认日期??

【问题讨论】:

    标签: javascript jquery asp.net datepicker


    【解决方案1】:

    问题是您正在使用用户控件的父级(承载用户控件的.aspx 页面)来获取 ASP.NET Session 对象。然后通过这个属性逻辑将此会话对象用于用户控件的所有 10 个实例:

    public DateTime ViewDate
    {
        get
        {
            return Parent.SessionObj.ViewDate;
        }
        set
        {
            Parent.SessionObj.ViewDate = value;
        }
    }
    

    用户控件(子控件)与父控件紧密耦合通常是一个坏主意。如果您尝试在 SessionObj 不存在的情况下使用此用户控件,那么它显然会以惊人的方式爆炸。这严重限制了所述用户控件的可重用性。

    您希望父级(.aspx 页面)告诉子级(.ascx 用户控件)ViewDate 的值是什么。在您发布的代码中,您的子控件询问父控件“嘿,会话缓存中查看日期的值是多少?”。

    将您的用户控制代码更改为类似以下内容:

    private DateTime myViewDate;
    
    public DateTime ViewDate
    {
        get
        {
            return myViewDate;
        }
        set
        {
            myViewDate = value;
        }
    }
    

    现在在应用用户控件的页面中,最初将ViewDate 属性的值设置为会话值,然后在该页面实际修改值时通过属性的设置器更新需要更改的一个实例。

    【讨论】:

    • 嗨 Karl,没错,任何 Parent 上的任何更改都会反映到所有 10 个实例上,目前我正在处理的页面是唯一需要更改的页面,其他 9 个实例只是为了显示日历,不需要任何选择。
    • @Manjuboyz - 因此您不能使用父会话对象作为用户控件ViewDate 属性的基础。您需要更改您的用户控制代码以拥有一个支持变量来保存ViewDate 值,并将您的gettersetter 更新为return 该支持变量并将传入的值分别分配给该支持变量。
    • myViewDate 默认设置为 01/01/0001,并且不接受将会话日期设置为它??我还缺少什么吗?
    • 我使用 myViewDate = DateTime.Today; 将日期设置为今天日期 :)它现在显示正确,将进行更多验证以检查它是否正常工作,如果我遇到任何其他问题会回复您,谢谢
    • @Manjuboyz - 是的,请确保您将日期变量实际初始化为实际日期;否则 .NET 使用最小日期值,即 0001 年 1 月 1 日;对日历没用!哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2021-07-08
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多