单击ASP.NET日历控件单元格选择日期

单击ASP.NET日历控件老是选择数字才能选择这个日期,当数字很小的时候选择很不方便,以下方式可以单击单元格来选择日期:

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppCalendar._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:Calendar ID="Calendar1" runat="server" Height="212px" ondayrender="Calendar1_DayRender" Width="390px"></asp:Calendar> </div> </form> </body> </html>

Default.aspx.cs代码:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebAppCalendar { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { //为日历单元格添加onClick事件 e.Cell.Attributes.Add("onClick", e.SelectUrl); //鼠标变为手型 e.Cell.Attributes.Add("onmouseover", "if(true) this.style.cursor='hand';"); } } }

运行结果:

单击ASP.NET日历控件单元格选择日期