Hello.
Using NetAdvantage 8.1 (2008 Volume1), how do I disable dates using server code? I want to iterate over all the dates in the calendar and enable/disable based on a datatable that contains "allowable" dates.
Thanks,
Mike
Hi Mike,
WebCalendar has no options to customize specific days on server. However, it has ClientSideEvents related to selection and rendering. So, you may build on server array which contains specific dates and on client process that array within selection and rendering. Below example, will paint specific dates with light gray background and will not allow to select them by user.Notes:1. Dates are passed to client as array of [year,month,day] fields. You may use different structure.2. Script is added as literal control to header. That will work only under full postback, because header may not be accessible under async postback. You may register that script with ScriptManager/Page/CallBackManager, or use other techniques.
aspx:
<script type="text/javascript">function WebCalendar1_ValueChanging(oCalendar, oDate, oEvent){ if(oDate && isDisabledDay(oDate.getFullYear(), oDate.getMonth() + 1, oDate.getDate())) oEvent.cancel = true;}function WebCalendar1_RenderDay(oCalendar, oDay, oEvent){ var disabled = isDisabledDay(oDay.year, oDay.month, oDay.day); var style = oDay.element.style; if(disabled) style.backgroundColor = "#E0E0E0"; else if(style.backgroundColor) { if(style.removeAttribute) style.removeAttribute("backgroundColor") else if(style.removeProperty) style.removeProperty("backgroundColor"); else style.backgroundColor = ""; }}function isDisabledDay(year, month, day){ if(typeof myDisabledDays != 'object') return false; var i = myDisabledDays.length; while(i-- > 0) { var date = myDisabledDays[i]; if(date[0] == year && date[1] == month && date[2] == day) return true; } return false;}</script>
<igsch:WebCalendar ID="WebCalendar1" runat="server"> <ClientSideEvents ValueChanging="WebCalendar1_ValueChanging" RenderDay="WebCalendar1_RenderDay"></ClientSideEvents></igsch:WebCalendar>
aspx.cs:
protected void Page_Load(object sender, EventArgs e){ System.Text.StringBuilder days = new System.Text.StringBuilder(); days.Append("window[\"myDisabledDays\"]=["); days.Append("[2008,1,1]"); days.Append(",[2008,2,19]"); days.Append(",[2008,5,11]"); days.Append(",[2008,6,20]"); days.Append(",[2008,7,4]"); days.Append("];"); days.Insert(0, "<script type=\"text/javascript\">").Append("</script>"); this.Page.Header.Controls.Add(new LiteralControl(days.ToString()));}