locked
How To Highlight My Date From my below code RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Yearly Calendar</title>
        <style type="text/css">
            table td { padding:0; margin:0; border:1px solid #dadada; }
            table th { width:25px; text-align:center; }
            table td { text-align:center; }
            table td span { color:#dadada; }
            table td a { color:#000000; text-decoration:none; }
            table td a:hover { text-decoration:underline; }
            table td a.hasEvents { color:#ff0000; }
            table td a.selected { color:#0000ff; font-weight:bold; }
            table td.month { background-color:#999999; }
            table td.weekend { background-color:#D0D0D0; }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <th>
                            <asp:Literal ID="litSelectedYear" runat="server" />
                        </th>
                        <% for (int iWeek = 1; iWeek <= 6; iWeek++) {
                            for (int iDay = 1; iDay <= 7; iDay++) { if (iWeek == 6 && iDay > 2) break; %>
                            <th>
                                <%= GetDayName(iDay) %>
                            </th>
                        <% } } %>
                    </tr>
                </thead>
                <asp:Repeater ID="repMonths" runat="server" OnInit="repMonths_OnInit" OnItemDataBound="repMonths_OnItemDataBound">
                    <ItemTemplate>                  
                        <tr>
                            <td class="month">
                                <asp:HyperLink ID="hylMonth" runat="server" />
                            </td>
                            <asp:Repeater ID="repDays" runat="server" OnItemDataBound="repDays_OnItemDataBound">
                                <ItemTemplate>
                                    <td id="tdDay" runat="server">
                                        <asp:Literal ID="litDay" runat="server" />
                                    </td>
                                </ItemTemplate>
                            </asp:Repeater>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
        </form>
    </body>
    </html>
    

    This is my code file

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class Default2 : System.Web.UI.Page
    {
        private DateTime _dtMonth;
        private DateTime _selectedDate;
        private bool _specialDaySelected = true;
        private int _currentBindingMonth;
    
        protected void repMonths_OnInit(object sender, EventArgs e)
        {
            if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
            {
                _specialDaySelected = false;
                int selectedMonth, selectedYear;
                int.TryParse(Request.QueryString["CalYear"], out selectedYear);
                int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);
    
                if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
                if (selectedMonth <= 1) selectedMonth = 1;
                else if (selectedMonth > 12) selectedMonth = 12;
    
                _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
            }
    
            litSelectedYear.Text = _selectedDate.Year.ToString();
    
            repMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            repMonths.DataBind();
        }
    
        protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repDays = (Repeater)e.Item.FindControl("repDays");
                HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
                _currentBindingMonth = (int)e.Item.DataItem;
                _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);
    
                hylMonth.Text = _dtMonth.ToString("MMM");
                hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);
    
                if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");
    
                if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
                {
                    int daysToSubtract = -(int)_dtMonth.DayOfWeek;
    
                    if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.
    
                    _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
                }
    
                DateTime[] dates = new DateTime[37];
                for (int i = 0; i < 37; i++)
                {
                    dates[i] = _dtMonth;
                    _dtMonth = _dtMonth.AddDays(1);
                }
    
                repDays.DataSource = dates;
                repDays.DataBind();
            }
        }
    
        protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DateTime date = (DateTime)e.Item.DataItem;
                Literal litDay = (Literal)e.Item.FindControl("litDay");
                HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
    
                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                    tdDay.Attributes.Add("class", "weekend");
    
                if (_currentBindingMonth == date.Month)
                    litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
                else
                    litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
    
                // Clear ID's
                tdDay.ID = string.Empty;
            }
        }
    
        public static string GetDayName(int dayInWeek)
        {
            switch (dayInWeek)
            {
                case 1: return "mo";
                case 2: return "tu";
                case 3: return "we";
                case 4: return "th";
                case 5: return "fr";
                case 6: return "sa";
                case 7: return "su";
            }
    
            return "God only made seven days in a week.";
        }
    
    }
    

    My Tabledata is like this

    BookingDate Field

    2018-03-02 00:00:00.000

    2018-03-16 00:00:00.000

    2018-04-10 00:00:00.000

    2018-05-05 00:00:00.000

    2018-06-20 00:00:00.000

    2018-11-25 00:00:00.000

    2019-01-20 00:00:00.000

    I want to highlight this date how to do so...

    Wednesday, August 8, 2018 9:43 AM

Answers

  • User-369506445 posted

    ok, I created a sample that fetches from the database, just you have changed my filed and table name to yours, I highlighted them in the code

    also create a field in your table with <g class="gr_ gr_204 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="204" data-gr-id="204">nvarchar</g> type and insert below values 

                2018-03-02 00:00:00.000
                2018-03-16 00:00:00.000
                2018-04-10 00:00:00.000
                2018-05-05 00:00:00.000
                2018-06-20 00:00:00.000
                2018-11-25 00:00:00.000
                2019-01-20 00:00:00.000
     private DateTime _dtMonth;
            private DateTime _selectedDate;
            private bool _specialDaySelected = true;
            private int _currentBindingMonth;
      
            protected void repMonths_OnInit(object sender, EventArgs e)
            {
                if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
                {
                    _specialDaySelected = false;
                    int selectedMonth, selectedYear;
                    int.TryParse(Request.QueryString["CalYear"], out selectedYear);
                    int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);
    
                    if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
                    if (selectedMonth <= 1) selectedMonth = 1;
                    else if (selectedMonth > 12) selectedMonth = 12;
    
                    _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
                }
    
                litSelectedYear.Text = _selectedDate.Year.ToString();
    
                repMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
                repMonths.DataBind();
            }
    
            protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater repDays = (Repeater)e.Item.FindControl("repDays");
                    HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
                    _currentBindingMonth = (int)e.Item.DataItem;
                    _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);
    
                    hylMonth.Text = _dtMonth.ToString("MMM");
                    hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);
    
                    if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");
    
                    if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
                    {
                        int daysToSubtract = -(int)_dtMonth.DayOfWeek;
    
                        if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.
    
                        _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
                    }
    
                    DateTime[] dates = new DateTime[37];
                    for (int i = 0; i < 37; i++)
                    {
                        dates[i] = _dtMonth;
                        _dtMonth = _dtMonth.AddDays(1);
                    }
    
                    repDays.DataSource = dates;
                    repDays.DataBind();
                }
            }
    
            protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DateTime date = (DateTime)e.Item.DataItem;
                    Literal litDay = (Literal)e.Item.FindControl("litDay");
                    HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
    
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                        tdDay.Attributes.Add("class", "weekend");
    
                   
                    if (_currentBindingMonth == date.Month)
                        litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
                    else
                        litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
    
                    changeColor(litDay, date);
                    // Clear ID's
                    tdDay.ID = string.Empty;
                }
            }
            public List<string> FetchFromDB()
            {
                List<string> list = new List<string>();
    
                string connetionString = "Data Source=.;Initial Catalog=ASPForum;Integrated Security=true";
                string sql = "select yourFiled from yourTable";
                using (var con = new SqlConnection(connetionString))
                {
                    using (var com = new SqlCommand(sql, con))
                    {
                        con.Open();
                        using (var rd = com.ExecuteReader())
                        {
                            while (rd.Read())
                            {
                                list.Add(rd.GetValue(0).ToString());
                            }
                        }
                    }
                }
    
                return list;
                
            }
            public void changeColor(Literal literal,DateTime date)
            {
                
                int q = FetchFromDB().Count(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    
            }
    
            public static string GetDayName(int dayInWeek)
            {
                switch (dayInWeek)
                {
                    case 1: return "mo";
                    case 2: return "tu";
                    case 3: return "we";
                    case 4: return "th";
                    case 5: return "fr";
                    case 6: return "sa";
                    case 7: return "su";
                }
    
                return "God only made seven days in a week.";
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 8, 2018 12:33 PM

All replies

  • User-369506445 posted

    hi

    I create a sample, please try it

    First create a method below like :

    public void changeColor(Literal literal,DateTime date)
            {
                
                var q = list.Where(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q.Count() > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    
            }

    and then call the method in  repDays_OnItemDataBound event

    List<string> list = new List<string>()
    {
    "2018-03-02 00:00:00.000",
    "2018-03-16 00:00:00.000",
    "2018-04-10 00:00:00.000",
    "2018-05-05 00:00:00.000",
    "2018-06-20 00:00:00.000",
    "2018-11-25 00:00:00.000",
    "2019-01-20 00:00:00.000",
    };

    protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DateTime date = (DateTime)e.Item.DataItem; Literal litDay = (Literal)e.Item.FindControl("litDay"); HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay"); if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) tdDay.Attributes.Add("class", "weekend"); if (_currentBindingMonth == date.Month) litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty); else litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2")); changeColor(litDay, date); // Clear ID's tdDay.ID = string.Empty; } }

    Wednesday, August 8, 2018 10:33 AM
  • User-807418713 posted

    hello

    im using asp.net2.0 C#

    so getting error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1002: ; expected

    Source Error:

    Line 74:         }
    Line 75:     }
    Line 76:    List<string> list = new List<string>()
    Line 77:         {
    Line 78:             "2018-03-02 00:00:00.000",

    Wednesday, August 8, 2018 10:43 AM
  • User-369506445 posted

    I set a <g data-gr-id="3" id="3" class="gr_ gr_3 gr-alert gr_spell ContextualSpelling ins-del multiReplace">hardocde</g> data as a list, you have to remove the list and replace your  DataTable

    Wednesday, August 8, 2018 10:51 AM
  • User-807418713 posted

    Hello

    can you please give me complete code with  datatable

    Wednesday, August 8, 2018 10:53 AM
  • User-807418713 posted

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1525: Invalid expression term '>'

    Source Error:

    Line 42:         {
    Line 43:             
    Line 44:             var q = list.Where(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
    Line 45:             if (q.Count() > 0)
    Line 46:                 literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);

    Wednesday, August 8, 2018 10:54 AM
  • User-369506445 posted

    please try below code

           private DateTime _dtMonth;
            private DateTime _selectedDate;
            private bool _specialDaySelected = true;
            private int _currentBindingMonth;
    
            List<string> list = new List<string>()
            {
                "2018-03-02 00:00:00.000",
                "2018-03-16 00:00:00.000",
                "2018-04-10 00:00:00.000",
                "2018-05-05 00:00:00.000",
                "2018-06-20 00:00:00.000",
                "2018-11-25 00:00:00.000",
                "2019-01-20 00:00:00.000",
            };
              
    
            protected void repMonths_OnInit(object sender, EventArgs e)
            {
                if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
                {
                    _specialDaySelected = false;
                    int selectedMonth, selectedYear;
                    int.TryParse(Request.QueryString["CalYear"], out selectedYear);
                    int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);
    
                    if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
                    if (selectedMonth <= 1) selectedMonth = 1;
                    else if (selectedMonth > 12) selectedMonth = 12;
    
                    _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
                }
    
                litSelectedYear.Text = _selectedDate.Year.ToString();
    
                repMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
                repMonths.DataBind();
            }
    
            protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater repDays = (Repeater)e.Item.FindControl("repDays");
                    HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
                    _currentBindingMonth = (int)e.Item.DataItem;
                    _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);
    
                    hylMonth.Text = _dtMonth.ToString("MMM");
                    hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);
    
                    if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");
    
                    if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
                    {
                        int daysToSubtract = -(int)_dtMonth.DayOfWeek;
    
                        if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.
    
                        _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
                    }
    
                    DateTime[] dates = new DateTime[37];
                    for (int i = 0; i < 37; i++)
                    {
                        dates[i] = _dtMonth;
                        _dtMonth = _dtMonth.AddDays(1);
                    }
    
                    repDays.DataSource = dates;
                    repDays.DataBind();
                }
            }
    
            protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DateTime date = (DateTime)e.Item.DataItem;
                    Literal litDay = (Literal)e.Item.FindControl("litDay");
                    HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
    
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                        tdDay.Attributes.Add("class", "weekend");
    
                   
                    if (_currentBindingMonth == date.Month)
                        litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
                    else
                        litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
    
                    changeColor(litDay, date);
                    // Clear ID's
                    tdDay.ID = string.Empty;
                }
            }
    
            public void changeColor(Literal literal,DateTime date)
            {
                
                var q = list.Where(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q.Count() > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    
            }
    
            public static string GetDayName(int dayInWeek)
            {
                switch (dayInWeek)
                {
                    case 1: return "mo";
                    case 2: return "tu";
                    case 3: return "we";
                    case 4: return "th";
                    case 5: return "fr";
                    case 6: return "sa";
                    case 7: return "su";
                }
    
                return "God only made seven days in a week.";
            }
        }

    Wednesday, August 8, 2018 11:48 AM
  • User-807418713 posted

    error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1002: ; expected

    Source Error:

    Line 17:         private int _currentBindingMonth;
    Line 18: 
    Line 19:         List<string> list = new List<string>()
    Line 20:         {
    Line 21:             "2018-03-02 00:00:00.000",

    Wednesday, August 8, 2018 12:05 PM
  • User-369506445 posted

    please put here your complete code

    Wednesday, August 8, 2018 12:10 PM
  • User-807418713 posted

    Hello

    My aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Yearly Calendar</title>
        <style type="text/css">
            table td { padding:0; margin:0; border:1px solid #dadada; }
            table th { width:80px;  text-align:center; }
            table td { height:50px; text-align:center; }
            table td span { color:#dadada; }
            table td a { color:#000000; text-decoration:none; }
            table td a:hover { text-decoration:underline; }
            table td a.hasEvents { color:#ff0000; }
            table td a.selected { color:#0000ff; font-weight:bold; }
            table td.month { background-color:#999999; }
            table td.weekend { background-color:#D0D0D0; }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <th>
                            <asp:Literal ID="litSelectedYear" runat="server" />
                        </th>
                        <% for (int iWeek = 1; iWeek <= 6; iWeek++) {
                            for (int iDay = 1; iDay <= 7; iDay++) { if (iWeek == 6 && iDay > 2) break; %>
                            <th>
                                <%= GetDayName(iDay) %>
                            </th>
                        <% } } %>
                    </tr>
                </thead>
                <asp:Repeater ID="repMonths" runat="server" OnInit="repMonths_OnInit" OnItemDataBound="repMonths_OnItemDataBound">
                    <ItemTemplate>                  
                        <tr>
                            <td class="month">
                                <asp:HyperLink ID="hylMonth" runat="server" />
                            </td>
                            <asp:Repeater ID="repDays" runat="server" OnItemDataBound="repDays_OnItemDataBound">
                                <ItemTemplate>
                                    <td id="tdDay" runat="server">
                                        <asp:Literal ID="litDay" runat="server" />
                                    </td>
                                </ItemTemplate>
                            </asp:Repeater>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
        </form>
    </body>
    </html>
    

    my codebehind

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Collections.Generic;
    
    public partial class Default2 : System.Web.UI.Page
    {
        private DateTime _dtMonth;
            private DateTime _selectedDate;
            private bool _specialDaySelected = true;
            private int _currentBindingMonth;
    
            List<string> list = new List<string>()
            {
                "2018-03-02 00:00:00.000",
                "2018-03-16 00:00:00.000",
                "2018-04-10 00:00:00.000",
                "2018-05-05 00:00:00.000",
                "2018-06-20 00:00:00.000",
                "2018-11-25 00:00:00.000",
                "2019-01-20 00:00:00.000",
            };
              
    
            protected void repMonths_OnInit(object sender, EventArgs e)
            {
                if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
                {
                    _specialDaySelected = false;
                    int selectedMonth, selectedYear;
                    int.TryParse(Request.QueryString["CalYear"], out selectedYear);
                    int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);
    
                    if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
                    if (selectedMonth <= 1) selectedMonth = 1;
                    else if (selectedMonth > 12) selectedMonth = 12;
    
                    _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
                }
    
                litSelectedYear.Text = _selectedDate.Year.ToString();
    
                repMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
                repMonths.DataBind();
            }
    
            protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater repDays = (Repeater)e.Item.FindControl("repDays");
                    HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
                    _currentBindingMonth = (int)e.Item.DataItem;
                    _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);
    
                    hylMonth.Text = _dtMonth.ToString("MMM");
                    hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);
    
                    if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");
    
                    if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
                    {
                        int daysToSubtract = -(int)_dtMonth.DayOfWeek;
    
                        if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.
    
                        _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
                    }
    
                    DateTime[] dates = new DateTime[37];
                    for (int i = 0; i < 37; i++)
                    {
                        dates[i] = _dtMonth;
                        _dtMonth = _dtMonth.AddDays(1);
                    }
    
                    repDays.DataSource = dates;
                    repDays.DataBind();
                }
            }
    
            protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DateTime date = (DateTime)e.Item.DataItem;
                    Literal litDay = (Literal)e.Item.FindControl("litDay");
                    HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
    
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                        tdDay.Attributes.Add("class", "weekend");
    
                   
                    if (_currentBindingMonth == date.Month)
                        litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
                    else
                        litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
    
                    changeColor(litDay, date);
                    // Clear ID's
                    tdDay.ID = string.Empty;
                }
            }
    
            public void changeColor(Literal literal,DateTime date)
            {
                
                var q = list.Where(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q.Count() > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    
            }
    
            public static string GetDayName(int dayInWeek)
            {
                switch (dayInWeek)
                {
                    case 1: return "mo";
                    case 2: return "tu";
                    case 3: return "we";
                    case 4: return "th";
                    case 5: return "fr";
                    case 6: return "sa";
                    case 7: return "su";
                }
    
                return "God only made seven days in a week.";
            }
        }
    
    }
    

    I'm using asp.net 2.0 c# what to use instead of var

    Wednesday, August 8, 2018 12:16 PM
  • User-369506445 posted

    you can use <g class="gr_ gr_29 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="29" data-gr-id="29">int</g> instead of <g class="gr_ gr_34 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="34" data-gr-id="34">var </g><g class="gr_ gr_34 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="34" data-gr-id="34">,</g> please try below

    int q = list.Count(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);

    Wednesday, August 8, 2018 12:23 PM
  • User-807418713 posted

    error again

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1002: ; expected

    Source Error:

    Line 19:         private int _currentBindingMonth;
    Line 20: 
    Line 21:         List<string> list = new List<string>()
    Line 22:         {
    Line 23:             "2018-03-02 00:00:00.000",

    Wednesday, August 8, 2018 12:25 PM
  • User-369506445 posted

    ok, I created a sample that fetches from the database, just you have changed my filed and table name to yours, I highlighted them in the code

    also create a field in your table with <g class="gr_ gr_204 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="204" data-gr-id="204">nvarchar</g> type and insert below values 

                2018-03-02 00:00:00.000
                2018-03-16 00:00:00.000
                2018-04-10 00:00:00.000
                2018-05-05 00:00:00.000
                2018-06-20 00:00:00.000
                2018-11-25 00:00:00.000
                2019-01-20 00:00:00.000
     private DateTime _dtMonth;
            private DateTime _selectedDate;
            private bool _specialDaySelected = true;
            private int _currentBindingMonth;
      
            protected void repMonths_OnInit(object sender, EventArgs e)
            {
                if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
                {
                    _specialDaySelected = false;
                    int selectedMonth, selectedYear;
                    int.TryParse(Request.QueryString["CalYear"], out selectedYear);
                    int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);
    
                    if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
                    if (selectedMonth <= 1) selectedMonth = 1;
                    else if (selectedMonth > 12) selectedMonth = 12;
    
                    _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
                }
    
                litSelectedYear.Text = _selectedDate.Year.ToString();
    
                repMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
                repMonths.DataBind();
            }
    
            protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater repDays = (Repeater)e.Item.FindControl("repDays");
                    HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
                    _currentBindingMonth = (int)e.Item.DataItem;
                    _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);
    
                    hylMonth.Text = _dtMonth.ToString("MMM");
                    hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);
    
                    if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");
    
                    if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
                    {
                        int daysToSubtract = -(int)_dtMonth.DayOfWeek;
    
                        if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.
    
                        _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
                    }
    
                    DateTime[] dates = new DateTime[37];
                    for (int i = 0; i < 37; i++)
                    {
                        dates[i] = _dtMonth;
                        _dtMonth = _dtMonth.AddDays(1);
                    }
    
                    repDays.DataSource = dates;
                    repDays.DataBind();
                }
            }
    
            protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DateTime date = (DateTime)e.Item.DataItem;
                    Literal litDay = (Literal)e.Item.FindControl("litDay");
                    HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
    
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                        tdDay.Attributes.Add("class", "weekend");
    
                   
                    if (_currentBindingMonth == date.Month)
                        litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
                    else
                        litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
    
                    changeColor(litDay, date);
                    // Clear ID's
                    tdDay.ID = string.Empty;
                }
            }
            public List<string> FetchFromDB()
            {
                List<string> list = new List<string>();
    
                string connetionString = "Data Source=.;Initial Catalog=ASPForum;Integrated Security=true";
                string sql = "select yourFiled from yourTable";
                using (var con = new SqlConnection(connetionString))
                {
                    using (var com = new SqlCommand(sql, con))
                    {
                        con.Open();
                        using (var rd = com.ExecuteReader())
                        {
                            while (rd.Read())
                            {
                                list.Add(rd.GetValue(0).ToString());
                            }
                        }
                    }
                }
    
                return list;
                
            }
            public void changeColor(Literal literal,DateTime date)
            {
                
                int q = FetchFromDB().Count(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
                if (q > 0)
                    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    
            }
    
            public static string GetDayName(int dayInWeek)
            {
                switch (dayInWeek)
                {
                    case 1: return "mo";
                    case 2: return "tu";
                    case 3: return "we";
                    case 4: return "th";
                    case 5: return "fr";
                    case 6: return "sa";
                    case 7: return "su";
                }
    
                return "God only made seven days in a week.";
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 8, 2018 12:33 PM
  • User-807418713 posted

    Hello

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1525: Invalid expression term '>'

    Source Error:

    Line 129:        {
    Line 130:            
    Line 131:            int q = FetchFromDB().Count(x => Convert.ToDateTime(x).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"));
    Line 132:            if (q > 0)
    Line 133:                literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);

    Wednesday, August 8, 2018 12:39 PM
  • User-369506445 posted

    please change the changeColor function to

    public void changeColor(Literal literal,DateTime date)
            {
                List<string> list = FetchFromDB();
                for (int i = 0; i < list.Count(); i++)
                {
                    if (Convert.ToDateTime(list[i]).ToString("dd.MM.yyyy")== date.ToString("dd.MM.yyyy"))
                    {
                        literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
                        return;
                    }
                }
            }

    Wednesday, August 8, 2018 12:51 PM
  • User-807418713 posted

    Hello

    Again Error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0118: 'System.Collections.Generic.List<string>.Count' is a 'property' but is used like a 'method'

    Source Error:

    Line 129:    {
    Line 130:        List<string> list = FetchFromDB();
    Line 131:        for (int i = 0; i < list.Count(); i++)
    Line 132:        {
    Line 133:            if (Convert.ToDateTime(list[i]).ToString("dd.MM.yyyy") == date.ToString("dd.MM.yyyy"))

    Wednesday, August 8, 2018 12:54 PM
  • User-369506445 posted

    change this

    for (int i = 0; i < list.Count(); i++)

    to

    for (int i = 0; i < list.Count; i++)

    Wednesday, August 8, 2018 12:58 PM
  • User-807418713 posted

    Hello

    Why im getting this error

    Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.

    is this due to 

    public List<string> FetchFromDB()
    {
    List<string> list = new List<string>();

    In  my demo i have only 3 rows in table

    Wednesday, August 8, 2018 1:02 PM
  • User-369506445 posted

    this error is about your database, that often it happens when your connection is open and doesn't close  yet

    ok for this problem, you can open a new thread or search on google

    anyway for solve the main problem, you can try below and if it was correct then try with Database

    please change the FetchFromDB function to

    public List<string> FetchFromDB()
            {
                List<string> list = new List<string>();
                list.Add("2018-03-02 00:00:00.000");
                list.Add("2018-03-16 00:00:00.000");
                list.Add("2018-04-10 00:00:00.000");
                list.Add("2018-05-05 00:00:00.000");
                list.Add("2018-06-20 00:00:00.000");
                list.Add("2018-11-25 00:00:00.000");
                list.Add("2019-01-20 00:00:00.000");
               
    
                return list;
                
            }

    public void changeColor(Literal literal,DateTime date)
    {
    List<string> list = FetchFromDB();
    for (int i = 0; i < list.Count; i++)
    {
    if (Convert.ToDateTime(list[i]).ToString("dd.MM.yyyy")== date.ToString("dd.MM.yyyy"))
    {
    literal.Text = string.Format("<div style='background-color: red'>{0}</div>", literal.Text);
    return;
    }
    }
    }

    Wednesday, August 8, 2018 1:10 PM
  • User-807418713 posted

    Hello vahid bakkhi

    Thanks For your Code

    Thank you So much

    Wednesday, August 8, 2018 1:16 PM