哈哈,强烈要求表扬!小废物呢,赶紧出来,给我开庆功会。。。
呵呵,
mmiluy几天前在他的blog上问了个关于Calendar控件的弹出式选择日期的要求。管家小废物真是热心啊,表扬一下,Q我说,让我帮他解决一下,不巧的是,我刚整理完系统,准备做JAVA的课题了,把.net都卸掉了,这不,昨天刚装上,帮mmiluy看看这个问题。
其实,也没做什么,呵呵,在google上搜“myCalendar.displayCalendar”。。。不过,都是基于VS2003的,我改到了05下,基本上,就是做了个WEB UserControl,然后页面里加一下就OK。
基本上,mmiluy应该知道如何建立用户控件等知识,下面我直接贴代码了,呵呵,快捷实用。
popUpCalendar.ascx-----------------------------------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="popUpCalendar.ascx.cs" Inherits="popUpCalendar" %>
<asp:panel id="pnlCalendar" Width="302px" Height="126px" runat="server">
<asp:Calendar id="Calendar1" Width="307px" Height="164px" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" OnSelectionChanged="Calendar1_SelectionChanged" >
<TodayDayStyle BackColor="#FFFFC0"></TodayDayStyle>
<DayStyle Font-Size="8pt" Font-Names="Arial"></DayStyle>
<DayHeaderStyle Font-Size="10pt" Font-Underline="True" Font-Names="Arial" BorderStyle="None" BackColor="#E0E0E0"></DayHeaderStyle>
<SelectedDayStyle Font-Size="8pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="Navy"></SelectedDayStyle>
<TitleStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="Navy"></TitleStyle>
<OtherMonthDayStyle ForeColor="Gray"></OtherMonthDayStyle>
</asp:Calendar>
</asp:panel>
<asp:panel id="pnlCalendar" Width="302px" Height="126px" runat="server">
<asp:Calendar id="Calendar1" Width="307px" Height="164px" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" OnSelectionChanged="Calendar1_SelectionChanged" >
<TodayDayStyle BackColor="#FFFFC0"></TodayDayStyle>
<DayStyle Font-Size="8pt" Font-Names="Arial"></DayStyle>
<DayHeaderStyle Font-Size="10pt" Font-Underline="True" Font-Names="Arial" BorderStyle="None" BackColor="#E0E0E0"></DayHeaderStyle>
<SelectedDayStyle Font-Size="8pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="Navy"></SelectedDayStyle>
<TitleStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="Navy"></TitleStyle>
<OtherMonthDayStyle ForeColor="Gray"></OtherMonthDayStyle>
</asp:Calendar>
</asp:panel>
popUpCalendar.ascx.cs-----------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data;
using System.Configuration;
using System.Collections;
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 popUpCalendar : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void displayCalendar(string sCalToolText, DateTime dSelectedDate, string sDateFieldName, int iTop, int iLeft)
{
if (pnlCalendar.Visible == false)
{
pnlCalendar.Style["top"] = iTop.ToString();
pnlCalendar.Style["left"] = iLeft.ToString();
Calendar1.SelectedDate = dSelectedDate;
Calendar1.VisibleDate = dSelectedDate;
Calendar1.ToolTip = sCalToolText;
Calendar1.Attributes["SelectedField"] = sDateFieldName;
pnlCalendar.Visible = true;
}
else
hideCalendar();
}
{
if (pnlCalendar.Visible == false)
{
pnlCalendar.Style["top"] = iTop.ToString();
pnlCalendar.Style["left"] = iLeft.ToString();
Calendar1.SelectedDate = dSelectedDate;
Calendar1.VisibleDate = dSelectedDate;
Calendar1.ToolTip = sCalToolText;
Calendar1.Attributes["SelectedField"] = sDateFieldName;
pnlCalendar.Visible = true;
}
else
hideCalendar();
}
public void hideCalendar()
{
pnlCalendar.Visible = false;
}
{
pnlCalendar.Visible = false;
}
public void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
TextBox tbDate;
tbDate = (TextBox)Page.FindControl(Calendar1.Attributes["SelectedField"]);
if (tbDate != null)
{
tbDate.Text = Calendar1.SelectedDate.ToShortDateString();
}
hideCalendar();
}
}
{
TextBox tbDate;
tbDate = (TextBox)Page.FindControl(Calendar1.Attributes["SelectedField"]);
if (tbDate != null)
{
tbDate.Text = Calendar1.SelectedDate.ToShortDateString();
}
hideCalendar();
}
}
以上是用户控件部分。下面是页面的使用,基本上,mmiluy在csdn上得到的代码就是调用户控件部分的代码,当然,也可以不做成用户控件,而把这些都写在一个页面里,呵呵,强烈不推荐这种方式。
dispCalendar.aspx--------------------------------------------
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="dispCalendar.aspx.cs" Inherits="dispCalendar" %>
<%@ Register Src="popUpCalendar.ascx" TagName="popUpCalendar" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " [url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<%@ Register Src="popUpCalendar.ascx" TagName="popUpCalendar" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " [url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="
[url]http://www.w3.org/1999/xhtml[/url]" >
<head runat="server">
<title>无标题页</title>
</head>
<body >
<head runat="server">
<title>无标题页</title>
</head>
<body >
<form id="form1" runat="server" >
<table>
<tr>
<td width="75">
Select Date:
</td>
<td>
<asp:TextBox id="txtDate" runat="server" Width="86px"></asp:TextBox>
</td>
<td style="width: 31px">
<asp:Button ID="btnSel" runat="server" OnClick="btnSel_Click" Text="选择日期" /></td>
</tr>
</table>
<uc1:popUpCalendar ID="myCalendar" runat="server" />
</form>
</body>
</html>
<table>
<tr>
<td width="75">
Select Date:
</td>
<td>
<asp:TextBox id="txtDate" runat="server" Width="86px"></asp:TextBox>
</td>
<td style="width: 31px">
<asp:Button ID="btnSel" runat="server" OnClick="btnSel_Click" Text="选择日期" /></td>
</tr>
</table>
<uc1:popUpCalendar ID="myCalendar" runat="server" />
</form>
</body>
</html>
dispCalendar.aspx.cs-------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data;
using System.Configuration;
using System.Collections;
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 dispCalendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myCalendar.hideCalendar();
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myCalendar.hideCalendar();
}
}
protected void btnSel_Click(object sender, EventArgs e)
{
DateTime SelDate;
try
{
SelDate = DateTime.Parse(txtDate.Text);
}
catch
{
SelDate = DateTime.Now;
}
myCalendar.displayCalendar("选择日期", SelDate, "txtDate", 20, 160);
}
}
OVER。。。。
本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/10066,如需转载请自行联系原作者