将日期选择器值设置为特定页面
Setting date picker value to specific page
我有一个通用的日期选择器,这个日期选择器几乎有 10 页,因为默认日期选择器日期设置为当前日期,我有一个页面(10 页中的第 1 页)link ,当我 select link 时,它包含一些日期(未来或过去)所以我希望 link selected 日期显示在日期选择器控件上,目前我能够做到,我面临的问题是,相同的更改日期反映到我不需要的其他 9 页,我应该能够看到它们的默认日期,即当前日期。
代码:
具体页面的方法我设置到具体日期
protected void QuickListDateNavigate(object sender, CommandEventArgs e)
{
DateTime newDate = DateTime.Parse((string)e.CommandArgument);
this.Parent.SessionObj.ViewDate = newDate;
}
这是上面 selected 日期被设置为 link selected 日期以及其他 9 页 的地方。
Datepicker.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);
}
之前是注释行,它设置了当前日期,添加了下一个 link "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;
}
因此,一旦日期更改为我的要求,它就不会将其他页面设置回当前日期,有什么方法可以将更改后的日期用于特定页面,而将默认日期用于其他页面??
问题是您正在使用用户控件的父级(托管用户控件的 .aspx
页面)获取 ASP.NET Session
对象。然后,此会话对象通过此 属性 逻辑用于用户控件的所有 10 个实例:
public DateTime ViewDate
{
get
{
return Parent.SessionObj.ViewDate;
}
set
{
Parent.SessionObj.ViewDate = value;
}
}
用户控件(子控件)与父控件紧密耦合通常不是一个好主意。如果您尝试在 SessionObj
不存在的情况下使用此用户控件,那么它显然会以惊人的方式爆炸。这严重限制了所述用户控件的可重用性。
您希望父项(.aspx
页面)告诉子项(.ascx
用户控件)ViewDate
的值是什么。在您发布的代码中,您的子控件正在询问父控件 "hey, what is the value in session cache for view date?"。
将您的用户控制代码更改为如下内容:
private DateTime myViewDate;
public DateTime ViewDate
{
get
{
return myViewDate;
}
set
{
myViewDate = value;
}
}
现在在应用用户控件的页面中,首先将ViewDate
属性的值设置为会话值,然后通过[=32更新需要更改的一个实例=] 的 setter 当该页面实际修改值时。
我有一个通用的日期选择器,这个日期选择器几乎有 10 页,因为默认日期选择器日期设置为当前日期,我有一个页面(10 页中的第 1 页)link ,当我 select link 时,它包含一些日期(未来或过去)所以我希望 link selected 日期显示在日期选择器控件上,目前我能够做到,我面临的问题是,相同的更改日期反映到我不需要的其他 9 页,我应该能够看到它们的默认日期,即当前日期。
代码:
具体页面的方法我设置到具体日期
protected void QuickListDateNavigate(object sender, CommandEventArgs e)
{
DateTime newDate = DateTime.Parse((string)e.CommandArgument);
this.Parent.SessionObj.ViewDate = newDate;
}
这是上面 selected 日期被设置为 link selected 日期以及其他 9 页 的地方。
Datepicker.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);
}
之前是注释行,它设置了当前日期,添加了下一个 link "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;
}
因此,一旦日期更改为我的要求,它就不会将其他页面设置回当前日期,有什么方法可以将更改后的日期用于特定页面,而将默认日期用于其他页面??
问题是您正在使用用户控件的父级(托管用户控件的 .aspx
页面)获取 ASP.NET Session
对象。然后,此会话对象通过此 属性 逻辑用于用户控件的所有 10 个实例:
public DateTime ViewDate
{
get
{
return Parent.SessionObj.ViewDate;
}
set
{
Parent.SessionObj.ViewDate = value;
}
}
用户控件(子控件)与父控件紧密耦合通常不是一个好主意。如果您尝试在 SessionObj
不存在的情况下使用此用户控件,那么它显然会以惊人的方式爆炸。这严重限制了所述用户控件的可重用性。
您希望父项(.aspx
页面)告诉子项(.ascx
用户控件)ViewDate
的值是什么。在您发布的代码中,您的子控件正在询问父控件 "hey, what is the value in session cache for view date?"。
将您的用户控制代码更改为如下内容:
private DateTime myViewDate;
public DateTime ViewDate
{
get
{
return myViewDate;
}
set
{
myViewDate = value;
}
}
现在在应用用户控件的页面中,首先将ViewDate
属性的值设置为会话值,然后通过[=32更新需要更改的一个实例=] 的 setter 当该页面实际修改值时。