这个问题纠结了好久,一直想用select实现动态创建下拉列表的效果,但是select中没有AutoPostBack
所以调用onServerChange时没有实现跳转,
具体的大家可以参照一下这个网站:
下面我将用dropDownList实现动态创建下拉菜单和日历的选择:
后台代码部分:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private int year;
private int month;
private int day;
private String now_time;
protected void Page_Load(object sender, EventArgs e)
{
//Select1.Attributes.Add("onserverchange", Page.GetPostBackEventReference(Select1));
if (!IsPostBack)
{
initSelect();
now_time = year.ToString() + "-" + month.ToString() + "-" + day.ToString();
Select2.Items[10].Selected = true;//以为这里设置当前时间的左右各20天,所以去当前时间为10
this.Calendar1.SelectedDate = DateTime.Parse(now_time);
}
}
//当点击dropDownList时触发事件
protected void select_date(object sender, EventArgs e)
{
int select_year = Convert.ToInt32(Select2.Items[Select2.SelectedIndex].Value);
int select_month = Convert.ToInt32(Select1.Items[Select1.SelectedIndex].Value);
int select_day = DateTime.Now.Day;
Select1.Items[Select1.SelectedIndex].Selected = true;
String select_now_time = select_year.ToString() + "-" + select_month.ToString() + "-" + select_day.ToString();
this.Calendar1.Controls.Clear();
this.Calendar1.VisibleDate = DateTime.Parse(select_now_time);
}
//当选择日历时触发事件
protected void calendar_change(object sender, EventArgs e)
{
initSelect();
int calendar_year = this.Calendar1.SelectedDate.Year;
int calendar_month = this.Calendar1.SelectedDate.Month;
int calendar_day = this.Calendar1.SelectedDate.Day;
int now_year=DateTime.Now.Year;
initSelect();
//Select1.Items[Select1.SelectedIndex].Selected = false;
Select1.Items[calendar_month - 1].Selected = true;
//Select2.Items[Select1.SelectedIndex].Selected = false;
Select2.Items[calendar_year - now_year + 10].Selected = true;
}
//初始化以及创建色了select2的下拉列表
public void initSelect()
{
year = DateTime.Now.Year;
month = DateTime.Now.Month;
day = DateTime.Now.Day;
for (int j = 0; j < Select1.Items.Count; j++)
{
Select1.Items[j].Selected = false;
}
Select2.Items.Clear();
for (int i = year - 10; i < year + 10; i++)
{
ListItem obj = new ListItem();
obj.Text = i.ToString();
obj.Value = i.ToString();
obj.Selected = false;
Select2.Items.Add(obj);
}
}
}
前台代码部分:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="calendar_Test.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="Select1" runat="server" OnSelectedIndexChanged="select_date" AutoPostBack="true">
<asp:ListItem Value="1">一月</asp:ListItem>
<asp:ListItem Value="2">二月</asp:ListItem>
<asp:ListItem Value="3">三月</asp:ListItem>
<asp:ListItem Value="4">四月</asp:ListItem>
<asp:ListItem Value="5">五月</asp:ListItem>
<asp:ListItem Value="6">六月</asp:ListItem>
<asp:ListItem Value="7">七月</asp:ListItem>
<asp:ListItem Value="8">八月</asp:ListItem>
<asp:ListItem Value="9">九月</asp:ListItem>
<asp:ListItem Value="10">十月</asp:ListItem>
<asp:ListItem Value="11">十一月</asp:ListItem>
<asp:ListItem Value="12">十二月</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Select2" runat="server" OnSelectedIndexChanged="select_date" AutoPostBack="true">
</asp:DropDownList>
<br/>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black"
BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black"
Height="250px" NextPrevFormat="ShortMonth" OnSelectionChanged="calendar_change"
Visible="true" Width="330px">
<SelectedDayStyle BackColor="#333399" ForeColor="White" />
<TodayDayStyle BackColor="#999999" ForeColor="White" />
<DayStyle BackColor="#CCCCCC" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" Height="8pt" />
<TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" Font-Size="12pt"
ForeColor="White" Height="12pt" />
</asp:Calendar>
</div>
</form>
</body>
</html>
图片效果:
谢谢观赏!