这是借用了别人写的控件,自己用的时候把一些细节写清楚了,可以直接用了。
http://www.cnblogs.com/hzxcms/p/4014917.html
这是原博的地址。
1、首先要在<form id="form1" runat="server">下面加这么一条:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
然后就可以直接在前台添加控件了。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="requestedDeliveryDateTextBox" runat="server" Width="100" />
<asp:ImageButton id="imageButton" Width="36px" runat="server" ImageUrl="~/File/timg.jpg" AlternateText="calendar" OnClick="ImageButton_Click" CausesValidation="false" Height="25px" />
<br />
<div id="calendar" class="calendar" visible="false" runat="server">
<asp:Calendar ID="requestedDeliveryDateCalendar" runat="server" OnSelectionChanged="RequestedDeliveryDateCalendar_SelectionChanged" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
红色区域的地方是imagebuttton的地址,需要修改。
2、后台代码如下
/// 日期选择图标被点击
/// </summary>
protected void ImageButton_Click(object sender, EventArgs eventArgs)
{
//控制日历的显示与隐藏,点击imagebutton即可显示日历
calendar.Visible = !calendar.Visible;
}
/// <summary>
/// 选择日期,通过AJAX触发
/// </summary>
protected void RequestedDeliveryDateCalendar_SelectionChanged(object sender, EventArgs eventArgs)
{
requestedDeliveryDateTextBox.Text = requestedDeliveryDateCalendar.SelectedDate.ToShortDateString();
// 隐藏日历
calendar.Visible = false;
}
3、requestedDeliveryDateTextBox.Text 即为选择日期的值