首页 新闻 会员 周边 捐助

Calendar 与 DropdownList 绑定的问题

0
悬赏园豆:10 [已解决问题] 解决于 2009-11-14 14:31

Selected Date 分别是:today,yesterday,tomorrow

如何实现当我在下面的下拉菜单中选择时间时,上面的日历随之改变。

海风1998的主页 海风1998 | 初学一级 | 园豆:20
提问于:2009-11-11 16:40
< >
分享
最佳答案
0
Code
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"
CellPadding
="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor
="Black" Height="180px" Width="200px">
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<WeekendDayStyle BackColor="#FFFFCC" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<OtherMonthDayStyle ForeColor="#808080" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
选择时间:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Today</asp:ListItem>
<asp:ListItem Value="-1">yesterday</asp:ListItem>
<asp:ListItem Value="1">tomorrow</asp:ListItem>
</asp:DropDownList>

html代码,注意的是DorpDownList要把AutoPostBack设为True

下面是CS代码

Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Calendar1.SelectedDayStyle.BackColor = Color.Red;
this.Calendar1.SelectedDate = DateTime.Today;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Calendar1.SelectedDate = DateTime.Today.AddDays(Convert.ToInt32(this.DropDownList1.SelectedValue));
}
收获园豆:8
风浪 | 老鸟四级 |园豆:2996 | 2009-11-11 17:39
你的答案和书上的很相似:书上是这么写的: calendar1.SelectedDate= DateTime.Now.AddDays(Convert.ToDouble(DropDownList1.SelectedItem.Value)); 没有前面判断回发那段代码,也没有用this关键字,我想问判断回发和this是必须的吗?
海风1998 | 园豆:20 (初学一级) | 2009-11-13 11:12
其他回答(1)
0

这个业务逻辑很奇怪! 已经选择了还要再点一下,还不如放个文本框,直接获取dropdownlist的SelectedValue

不过,学习是可以的。其实很简单

 

DateTime tomorrow = DateTime.Today.AddDays(1);
Calendar1.TodaysDate
= tomorrow;
Calendar1.SelectedDate
= Calendar1.TodaysDate;

 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DateTime today
= System.DateTime.Today;
DateTime yesterday
= today.AddDays(-1);
DateTime tomorrow
= today.AddDays(1);
DropDownList1.Items.Add(String.Format(
"{0:dd MMM yyyy}",
today));
DropDownList1.Items.Add(String.Format(
"{0:dd MMM yyyy}",
yesterday));
DropDownList1.Items.Add(String.Format(
"{0:dd MMM yyyy}",
tomorrow));
}
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
Calendar1.TodaysDate
=
DateTime.Parse(DropDownList1.SelectedItem.Text);
}

 

http://msdn.microsoft.com/zh-cn/library/8k0f6h1h.aspx

收获园豆:2
邀月 | 园豆:25475 (高人七级) | 2009-11-11 17:03
看得不是很明白,为什么要把明天的值赋给今天。 Calendar1.TodaysDate = tomorrow;
支持(0) 反对(0) 海风1998 | 园豆:20 (初学一级) | 2009-11-13 11:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册