简体   繁体   English

如何在日历asp.net中用不同的颜色突出显示不同的日子

[英]How to highlight different days with different colors in calendar asp.net

i created calendar control that highlight the selected dates. 我创建了突出显示所选日期的日历控件。 but i dont know how to use different colors for each day. 但我不知道如何每天使用不同的颜色。

        <asp:Calendar ID="Calendar1" runat="server" 

            Height="365px" Width="594px" CssClass="myCalendar"
            OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged" >
            <DayHeaderStyle 
    CssClass="myCalendarDayHeader"/>
            <DayStyle CssClass="myCalendarDay" Font-Bold="True" Font-Names="Cordia New" 
                Font-Size="30px"/>
            <SelectedDayStyle 
    CssClass="myCalendarSelector" BackColor="#3366FF"/>
            <SelectorStyle Wrap="True" CssClass="myCalendarSelector"/>
            <TitleStyle 
    CssClass="myCalendarTitle" />
            <TodayDayStyle CssClass="myCalendarToday"/>
        </asp:Calendar>

.... ....

 public static List<DateTime> list = new List<DateTime>();
    Hashtable HolidayList;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
        foreach (DateTime dt in newList)
        {
            Calendar1.SelectedDates.Add(dt);
        }
        list.Clear();
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {

        if (e.Day.IsWeekend == true)
        {

            e.Cell.Enabled = false;
            e.Day.IsSelectable = false;
            e.Cell.BackColor = System.Drawing.Color.Gray;
        }

            if (e.Day.IsSelected == true)
            {
                list.Add(e.Day.Date);
                e.Day.IsSelectable = false;
            }
            Session["SelectedDates"] = list;


    }

i would like to add a radio button so that i can change color for example radio one is blue radio two is yellow. 我想添加一个单选按钮,以便我可以更改颜色,例如,收音机一是蓝色,收音机二是黄色。 can anyone have an idea? 谁能有个主意?

Ok, try this 好的,试试这个

Add a RadioButtonList with AutoPostBack="true" and OnSelectedIndexChanged event to your markup as follows. 如下所示,将带有AutoPostBack="true"OnSelectedIndexChanged事件的RadioButtonList添加到您的标记中。

<asp:Calendar ID="Calendar1" runat="server" Height="365px" Width="594px" CssClass="myCalendar"
            OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged" >
            <DayHeaderStyle 
    CssClass="myCalendarDayHeader"/>
            <DayStyle CssClass="myCalendarDay" Font-Bold="True" Font-Names="Cordia New" 
                Font-Size="30px"/>
            <SelectedDayStyle 
    CssClass="myCalendarSelector" BackColor="#3366FF"/>
            <SelectorStyle Wrap="True" CssClass="myCalendarSelector"/>
            <TitleStyle 
    CssClass="myCalendarTitle" />
            <TodayDayStyle CssClass="myCalendarToday"/>
        </asp:Calendar>

    <asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem>Red</asp:ListItem>
        <asp:ListItem>Yellow</asp:ListItem>
        <asp:ListItem>Green</asp:ListItem>
    </asp:RadioButtonList>

And, in your code behind implement the OnSelectedIndexChanged event as follows 并且,在后面的代码中,实现OnSelectedIndexChanged事件,如下所示

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Drawing.Color selColor;
            switch(RadioButtonList1.SelectedValue)
            {
                case "Red": selColor = System.Drawing.Color.Red;
                    break;
                case "Yellow": selColor = System.Drawing.Color.Yellow;
                    break;
                default: selColor = System.Drawing.Color.Green;
                    break;
            }
            Calendar1.SelectedDayStyle.BackColor = selColor;

        }

That's it! 而已! Your selected dates will get highlighted with the color you select in the RadioButtonList control. 您选择的日期将以您在RadioButtonList控件中选择的颜色突出显示。 Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据事件用不同颜色突出显示日历中的日期? - How to highlight the date in calendar with different colors according to events? 在jQuery UI Datepicker和asp.net日历上禁用自定义日期 - Disable custom days on jQuery UI Datepicker and asp.net calendar 如何在ASP.NET中突出显示活动的父级 - How to highlight active parent in ASP.NET 如何为HTML元素设置不同的颜色(自动突出显示)样式 - How to style different colors (auto highlight) to html elements 如何突出一个区域系列中不同colors的高低值? - How to highlight the high and low values with different colors in an area series? 如何用不同的颜色为从周六到周五的每周日历着色 - How to color each week of calendar from Sat to Fri in different colors 根据列的不同,用不同的颜色突出显示表格行 - Highlight table row with different colors depending on their column 如何使用 react-highlight-words 突出显示具有不同 colors 的多个关键字 - how to use react-highlight-words to highlight multiple keywords with different colors 如何在 v-calendar 中更改周末的 colors(周六和周日分别使用不同的 colors) - How to change the colors of weekends in v-calendar (different colors for saturdays and sundays respectively) 在不同的ASP.NET MVC上使用不同版本的jquery - Using different version of jquery on different ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM