adsense

Thursday, June 27, 2013

ASP.Net Clendar Hide WeekEnd

Asp.Net Calender control gives facility to hide the day cells by using the following code.

 e.Cell.Visible = !e.Day.IsWeekend;

Even though you can hide the day cell, Calender header will be still visible and it will show the Saturday and Sunday headings.

I have come across one post that would give some starting point to resolve this issues.

http://stackoverflow.com/questions/554111/how-can-i-hide-weekends-when-using-the-asp-net-calendar-control

As described in this post you will have to override the Calender control's render method to achieve the desired behavior. But this method did not work for me as it expects a XML compatible string.

In the same post as zacharydl have suggested I was able to achive the result using jQuery with a slight modification to the code. You will have to call the javascript function during the post back event to avoid showing the weekends when we change the selected month.

<script language="javascript">

 HideWeekEnd();
 
   
    function HideWeekEnd ()
    {
        $('._title').parent().attr('colspan', '7'); 
        $('.evenCal:nth-last-child(1) , .evenCal:nth-last-child(2) ', $('#ScheduleParentWeb_cpHolder_calSchedule')).hide(); // remove last two headings
        $('._weekendday').hide(); // remove all the cells marked weekends
    }

 Sys.Application.add_init(appl_init);

        function appl_init() {
            var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
            pgRegMgr.add_endRequest(HideWeekEnd);// calls the  HideWeekEnd funtion on postback
        }

    </script>

<asp:Calendar runat="server" ID="Calendar1">
    <TitleStyle CssClass="_title" />
    <DayHeaderStyle CssClass="evenCal" />
    <WeekendDayStyle CssClass="_weekendday" />
</asp:Calendar>

Finally add the css classes to a stylesheet.

Cheers
Samitha

No comments:

Post a Comment