更改日历日背颜色

时间:2018-02-23 18:01:24

标签: c# asp.net

我正在尝试从日历中保存一天数据,并在点击后将其颜色更改为红色。所以保存在数据库中的颜色为红色,未保存的颜色为绿色。

问题是,当我选择多天时,最后一个会变为红色,前一个会变成绿色。 正确地从sql接收数据我在文本框中显示它并且显示所有保存的天数

int[] arr;
dynamic ad;
protected void Page_Load(object sender, EventArgs e)
{

    cn.Open();
    SqlCommand c1 = new SqlCommand("select count(*) from app1", cn);

    int count = int.Parse(c1.ExecuteScalar().ToString());
    cn.Close();
     arr = new int[count];
     DataClassesDataContext db = new DataClassesDataContext();
        ad  =(from a in db.app1s  select a.data).ToArray();

}


protected void Button1_Click(object sender, EventArgs e)
{
    //id is int 

    SqlCommand cmd = new SqlCommand("insert into app1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList1.SelectedValue + "')", cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

}


protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    foreach (var item in ad)
    {
        if (int.Parse(item) == int.Parse(Calendar1.SelectedDate.Day.ToString()))
        {
            Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
        }
        else
        {
            Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Green;
            TextBox5.Text = Calendar1.SelectedDate.Day.ToString();
        }
    }



}

我尝试使用数组但问题相同

int[] arr;
protected void Page_Load(object sender, EventArgs e)
{

    cn.Open();
    SqlCommand c1 = new SqlCommand("select count(*) from apoyt1", cn);

    int count = int.Parse(c1.ExecuteScalar().ToString());
    cn.Close();
     arr = new int[count + 1];

    for (int i = 1; i <= count; i++)
    {
        cn.Open();

        SqlCommand cm = new SqlCommand("select data from apoyt1 where id='" + i + "'", cn);
        arr[i] = int.Parse(cm.ExecuteScalar().ToString());

        cn.Close();

    }


}


protected void Button1_Click(object sender, EventArgs e)
{
    //id is int 

    SqlCommand cmd = new SqlCommand("insert into apoyt1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList1.SelectedValue + "')", cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

}






protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    for (int i = 0; i < arr.Length; i++)
    {
        if (arr[i] == int.Parse(Calendar1.SelectedDate.Day.ToString()))
        {
            Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
        }
        else {
            Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Green;
            TextBox5.Text = Calendar1.SelectedDate.Day.ToString();
        }
    }



}

1 个答案:

答案 0 :(得分:0)

您的代码没有达到应该计划的目的,Calendar1.SelectedDayStyle.BackColor始终引用相同的选定对象。请尝试以下方法: 1)创建天数列表,因为天数未知。

 List<Int32> days;

      protected void Page_Load(object sender, EventArgs e)
      {              
      }

2)由于每次重新加载页面,您需要在会话中存储这些天。

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
      {
          days = (List<int>)Session["var"];
           if (days == null) days = new List<int>();
           days.Add(Calendar1.SelectedDate.Day);
           Session["var"] = days;
           ListBox1.Items.Clear();
              foreach (int d in days)
              {
                Calendar1.SelectedDates.Add(new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, d));
                Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
                ListBox1.Items.Add(d.ToString());
           }

      }

3)我上传了一个代码示例,以便您查看Here