2013-07-17 57 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; 
} 
} 
} 
相關問題