2013-07-17 30 views
0

我已经使用了下面的.net代码。其中显示了我在数据库中存储的假期日期。但是,当我将鼠标悬停在日期上时,我想显示一些消息。想在鼠标悬停在日历上的日期显示一条消息

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
string connection = @"server=TOPHAN-PC; Database=Tophan;uid=sa;pwd=123"; 
SqlConnection con = null; 
protected DataSet dsHolidays; 

protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 

    con = new SqlConnection(connection); 
    Calendar1.VisibleDate = DateTime.Today; 
    FillHolidayDataset(); 
} 
} 

protected void FillHolidayDataset() 
{ 
DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, 1); 
DateTime lastDate = GetFirstDayOfNextMonth(); 
dsHolidays = GetCurrentMonthData(firstDate, lastDate); 
} 

protected DateTime GetFirstDayOfNextMonth() 
{ 
int monthNumber, yearNumber; 
if (Calendar1.VisibleDate.Month == 12) 
{ 
    monthNumber = 1; 
    yearNumber = Calendar1.VisibleDate.Year + 1; 
} 
else 
{ 
    monthNumber = Calendar1.VisibleDate.Month + 1; 
    yearNumber = Calendar1.VisibleDate.Year; 
} 
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1); 
return lastDate; 
} 

protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate) 
{ 
DataSet dsMonth = new DataSet(); 
try 
{    
    //ConnectionStringSettings cs; 
    //cs = ConfigurationManager.ConnectionStrings["ConnectionString1"]; 
    //String connString = cs.ConnectionString; 
    //SqlConnection dbConnection = new SqlConnection(connString); 

    String query = "SELECT CDate FROM calender WHERE CDate >= @firstDate AND CDate <@lastDate"; 
    con.Open(); 
    SqlCommand dbCommand = new SqlCommand(query, con); 
    dbCommand.Parameters.Add(new SqlParameter("@firstDate", 
     firstDate)); 
    dbCommand.Parameters.Add(new SqlParameter("@lastDate", lastDate)); 

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand); 
    sqlDataAdapter.Fill(dsMonth);      
} 
catch 
{ } 
return dsMonth; 
} 
protected void Calendar1_VisibleMonthChanged(object sender,MonthChangedEventArgs e) 
{ 
FillHolidayDataset(); 
} 
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
{ 
try 
{ 
    DateTime nextDate; 
    if (dsHolidays != null) 
    { 
     foreach (DataRow dr in dsHolidays.Tables[0].Rows) 
     { 
      nextDate = (DateTime)dr["CDate"]; 
      if (nextDate == e.Day.Date) 
      { 
       e.Cell.BackColor = System.Drawing.Color.Pink; 
      } 
     } 
    } 
} 
catch 
{ } 
} 
} 

通过使用此代码,我发现它用颜色突出显示了日期,但我想在日期上移动鼠标时收到一些消息。

回答

0
foreach (DataRow dr in dsHolidays.Tables[0].Rows) 
    { 
     nextDate = (DateTime)dr["CDate"]; 
     if (nextDate == e.Day.Date) 
     { 
      e.Cell.BackColor = System.Drawing.Color.Pink; 
     e.Cell.ToolTip = "my tool tip"; 
     } 
    } 

希望这是明确的。

+0

不,我试过了,但它不会工作,它不会显示消息。 @Ratna – Sabyasachi

0
if (dsHolidays != null) 
    { 
     foreach (DataRow dr in dsHolidays.Tables[0].Rows) 
     { 
      nextDate = (DateTime)dr["CDate"]; 
      if (nextDate == e.Day.Date) 
      { 
e.Cell.ToolTip ="Add Message Here"; 
e.Cell.BackColor = Color.Yellow; 
e.Cell.ForeColor = Color.Red; 
} 
} 
} 
相关问题