[英]Asp calendar with datasource repeats the label
I have an asp calendar linked to datasource and it fetches the count of number of times a date has repeated in the data table and display it on the calendar. 我有一个链接到数据源的asp日历,它获取日期在数据表中重复的次数计数并将其显示在日历上。 but if there are 2 dates of the same month is on the mysql table then the labels appears twice and thrice if there are 3 dates and so on as shown in the image below.
但是,如果mysql表上有2个同一月的日期,则标签出现两次,如果有3个日期,则标签会出现三次,以此类推,如下图所示。
The code for this is shown below 此代码如下所示
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
cal2.VisibleDate = DateTime.Today;
FillLeaveplannerDataset();
}
}
protected void FillLeaveplannerDataset()
{
cal2.VisibleDate = cal2.TodaysDate;
DateTime firstDate = new DateTime(cal2.VisibleDate.Year, cal2.VisibleDate.Month, 1).AddDays(-6);
DateTime lastDate = new DateTime(cal2.VisibleDate.Date.AddMonths(1).Year, cal2.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7);
dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);
}
protected DateTime GetFirstDayOfNextMonth()
{
int monthNumber, yearNumber;
if (cal2.VisibleDate.Month == 12)
{
monthNumber = 1;
yearNumber = cal2.VisibleDate.Year + 1;
}
else
{
monthNumber = cal2.VisibleDate.Month + 1;
yearNumber = cal2.VisibleDate.Year;
}
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
return lastDate;
}
protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
{
DataSet dsMonth = new DataSet();
MySqlConnection con = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
string caldate = "Select date From approved Where date >= @firstDate And date <= @lastDate Group By date";
MySqlCommand cmd = new MySqlCommand(caldate, con);
cmd.Parameters.AddWithValue("@firstDate", firstDate);
cmd.Parameters.AddWithValue("@lastDate", lastDate);
MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter(cmd);
try
{
mysqlDataAdapter.Fill(dsMonth);
}
catch { }
return dsMonth;
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DateTime nextDate;
if (dsleaveplanner != null)
{
foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
{
nextDate = (DateTime)dr["date"];
MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
cmd2.Parameters.AddWithValue("@date", nextDate);
cmd3.Parameters.AddWithValue("@date", nextDate);
conn.Open();
string count = cmd2.ExecuteScalar().ToString();
string count2 = cmd3.ExecuteScalar().ToString();
var slot2 = Convert.ToInt32(count);
Int32 slot3 = 10 - slot2;
string slot4 = slot3.ToString();
conn.Close();
if (nextDate == e.Day.Date)
{
e.Cell.BackColor = System.Drawing.Color.Orange;
Environment.NewLine.ToString();
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
e.Cell.Controls.Add(new LiteralControl(slot4));
e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
e.Cell.Controls.Add(new LiteralControl(count2));
}
else
{
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p>Slot available: 10</p>"));
e.Cell.Controls.Add(new LiteralControl("<p></p>Pending: 0"));
}
}
}
}
protected void Calendar1_VisibleMonthChanged(object sender,
MonthChangedEventArgs e)
{
DateTime firstDate = e.NewDate.AddDays(-7);
DateTime lastDate = e.NewDate.AddMonths(1).AddDays(7);
dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);
}
What I want is if there is no data for a date i want the default value Slot avalable: 10 and Pending: 0 to be displayed. 我想要的是如果某个日期没有数据,我希望显示默认值Slot avalable:10和Pending:0。 And I want the slot available and pending to be displayed only once in each date.
而且我希望每个日期仅显示一次可用和待处理的广告位。 What am I missing here?
我在这里想念什么? Thanks in advance
提前致谢
First of all create one sample class with your name 首先用您的名字创建一个示例类
class Sample
{
public DateTime Date { get; set; }
public int SlotAvailable { get; set; }
public int Pending { get; set; }
}
And create a List<Sample>
并创建一个
List<Sample>
List<Sample> samples = new List<Sample>();
Then change your foreach
like 然后像这样改变你的
foreach
foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
{
nextDate = (DateTime)dr["date"];
MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
cmd2.Parameters.AddWithValue("@date", nextDate);
cmd3.Parameters.AddWithValue("@date", nextDate);
conn.Open();
string count = cmd2.ExecuteScalar().ToString();
string count2 = cmd3.ExecuteScalar().ToString();
var slot2 = Convert.ToInt32(count);
Int32 slot3 = 10 - slot2;
string slot4 = slot3.ToString();
conn.Close();
samples.Add(new Sample { Date = nextDate, SlotAvailable = slot4, Pending = count2 });
}
And then simply check if the e.Day.Date
is present in our date in List<Sample>
s date like 然后只需检查
List<Sample>
的日期中我们的日期中是否存在e.Day.Date
if (samples.Any(x => x.Date == e.Day.Date))
{
e.Cell.BackColor = System.Drawing.Color.Orange;
Environment.NewLine.ToString();
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));
}
else
{
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p>Slot available: 10</p>"));
e.Cell.Controls.Add(new LiteralControl("<p></p>Pending: 0"));
}
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.