title: ASP Calendar control to change background color code behind C#
body: <p>If dates are available in database table, then the Calendar background color should be changed as a red color base on available event dates in a SQL table.
  Below is my design code.</p>
  
  <pre><code>&lt;asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"&gt;
  &lt;/asp:Calendar&gt;
  Selected Date: &lt;asp:Label ID="Label1" runat="server" &gt;&lt;/asp:Label&gt;
  
   &lt;br /&gt;
   &lt;br /&gt;
  
    &lt;asp:GridView ID="gv_Regdetails" runat="server" AllowSorting="True"
                  AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid"
                  Width="900px" PageSize="50"&gt;
                  &lt;RowStyle HorizontalAlign="Center" /&gt;
                  &lt;AlternatingRowStyle CssClass="alt" /&gt;
                  &lt;Columns&gt;
                      &lt;asp:TemplateField HeaderText="Sno"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;%# Container.DataItemIndex + 1 %&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;HeaderStyle HorizontalAlign="Center" Width="30px" /&gt;
                          &lt;ItemStyle CssClass="th" HorizontalAlign="Center" Width="30px" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:BoundField DataField="EventName" HeaderText="EventName"&gt;
                          &lt;ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" /&gt;
                          &lt;HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" /&gt;
                      &lt;/asp:BoundField&gt;
                      &lt;asp:BoundField DataField="Title" HeaderText="Title"&gt;
                          &lt;ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" /&gt;
                          &lt;HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" /&gt;
                      &lt;/asp:BoundField&gt;
  
                      &lt;asp:BoundField DataField="Description" HeaderText="Description"&gt;
                          &lt;ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" /&gt;
                          &lt;HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" /&gt;
                      &lt;/asp:BoundField&gt;
  
                  &lt;/Columns&gt;
                  &lt;EmptyDataTemplate&gt;
                      &lt;center&gt;
                                      &lt;asp:Label ID="Label12" runat="server" Text="No Data avaliable for selected dates"
                                          ForeColor="Red"&gt;&lt;/asp:Label&gt;&lt;/center&gt;
                  &lt;/EmptyDataTemplate&gt;
              &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Below is my c# code and I am writing my logic in Calendar1_DayRender method.</p>
  
  <pre><code>protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  {        
      Label1.Text = Calendar1.SelectedDate.ToShortDateString();
      bo.Para1 = Label1.Text;
      DataTable dt = new DataTable();
      dt = bl.Get_detailsbydate(bo);
      gv_Regdetails.DataSource = dt;
      gv_Regdetails.DataBind();
  }
  protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
  {
      DataTable dt = new DataTable();
      dt = bl.Get_detailsbydate(bo);
      string dates = dt.Rows[0]["EventDate"].ToString();
  
   //   if (e.Day.Date == dates)
  //    { 
   //           e.Day.IsSelectable = false;
   //           e.Cell.ForeColor = System.Drawing.Color.Red;
  //    }
  
  
  }
  </code></pre>