简体   繁体   中英

Higlight/disable specific dates in ajax calendar control based on database selection

I need to use ajax calendar control to show dates from current date and disable & highlight dates stored in database table.

I am able to disable previous dates and limit selection to one month from current date and Also highlight the dates which are already booked for event in asp.net calendar control but not in ajax calendar control.

在此处输入图片说明

Following code generate error in ajaxcalander control:

   protected void AjaxCalendar_PreRender(object sender, EventArgs e)  
   {
        //startdate= enddate="2012-06-25"
        DateTime startDate = Helper.GetUAEDateTime();
        DateTime endDate = DateTime.Now.AddDays(10);

        AjaxCalendar.StartDate = startDate;
        AjaxCalendar.EndDate = endDate;

        // If the month is CurrentMonth
        if (!e.Day.IsOtherMonth)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ((dr["BookingDate"].ToString() != DBNull.Value.ToString()))
                {
                    DateTime dtEvent = (DateTime)dr["BookingDate"];
                    if (dtEvent.Equals(e.Day.Date))
                    {
                        e.Cell.BackColor = Color.PaleVioletRed;
                        e.Day.IsSelectable = false;

                    }
                }
            }
        }
        //If the month is not CurrentMonth then hide the Dates
        else
        {
            e.Cell.Text = "";
        }
    }
}

ERROR GENERATED BY following code if (!e.Day.IsOtherMonth)

CS1061: 'System.EventArgs' does not contain a definition for 'Day' and no extension method 'Day' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

HTML

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="AjaxCalendar" runat="server"  
     TargetControlID="TextBox2" Format="yyyy-MM-dd" 
     onprerender="AjaxCalendar_PreRender"  >
</asp:CalendarExtender>

if i put the same code block in asp.net calendar control it works fine

I have spend several hours looking for the solution or code sample which can help me resolve this issue with no success. I would appreciate any help on this.

I have looked at 100 of example but most of them show blocking old date, or weekend using javascript but i want to achieve it using code behind. It day rendering event available in ajax calendar control

<AjaxControl:CalendarExtender ID ="myCalendar" runat=”server” TargetControlID=”txtDateTime” OnClientShown="disablenonthursdays">
</AjaxControl:CalendarExtender>

<script language="javascript">
function disablenonthursdays(sender, args)
{
     for(var day =0; day <sender._days.all.length; day++)
    {
              for(var weekday =0; weekday < 6; weekday++)
             {
                    if(sender._days.all[day].id != “calendarValidToDate_day_”+ weekday +”_4″)
                   {
                          sender._days.all[day].disabled = true;
                          sender._days.all[day].innerHTML = “<div>” +sender._days.all[day].innerText+ “</div>”;
                    }
            }
     }
}
</script>

That is correct, because e is of type EventArgs witch contains information about the event. I don't know the actual type in the PreRender-Event, but you should be able to debug the code and need to cast e to the actual type to access the property 'Day'.

I think you can do that using Calandar control and PopupControl Extender . You can do all you do to Calendar control and it's act like Calendar Extender . check this link also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM