在asp.net日历中取消选择日期

时间:2014-04-03 13:23:31

标签: asp.net calendar c#-3.0

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Calendar.ascx.cs" Inherits="WebUserControl" %>
 <table>
    <tr>
    <td width="100%">

        <asp:DropDownList ID="ddlyear" runat="server" 
            onselectedindexchanged="ddlyear_SelectedIndexChanged" AutoPostBack="true">

                <asp:ListItem Text="2014" Value="2014" Selected="True"></asp:ListItem>
                <asp:ListItem Text="2015" Value="2015"></asp:ListItem>
                <asp:ListItem Text="2016" Value="2016"></asp:ListItem>
                <asp:ListItem Text="2017" Value="2017"></asp:ListItem>
                <asp:ListItem Text="2018" Value="2018"></asp:ListItem>
                <asp:ListItem Text="2019" Value="2019"></asp:ListItem>
                <asp:ListItem Text="2020" Value="2020"></asp:ListItem>
                <asp:ListItem Text="2021" Value="2021"></asp:ListItem>
                <asp:ListItem Text="2022" Value="2022"></asp:ListItem>
                <asp:ListItem Text="2023" Value="2023"></asp:ListItem>
                <asp:ListItem Text="2024" Value="2024"></asp:ListItem>

        </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td width="100%" dir="ltr">

        <asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" 
            RepeatDirection="Horizontal" RepeatColumns="4" 
            onitemdatabound="DataList1_ItemDataBound" 
          >
        <HeaderTemplate>

        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label ID="txt1" runat="server" Text='<%#Eval("Month") %>' Visible="false" Font-Names="Arial"></asp:Label>
            <asp:HiddenField ID="hdn1" runat="server" />
            <asp:Calendar ID="Calendar1" runat="server"  NextPrevFormat="CustomText"  SelectionMode="Day"    NextMonthText="" PrevMonthText=""    Font-Names="A" OtherMonthDayStyle-BorderStyle="NotSet" OtherMonthDayStyle-Wrap="False" OtherMonthDayStyle-ForeColor="#CCCCCC" >
            <TitleStyle  
                 BackColor="#6EC347" 
                 ForeColor="White"
                 Height="36"  
                 Font-Size="Large"  
                 Font-Names="Arial"  
                 />  
                  <SelectedDayStyle  
                 BackColor="Green"  
                 BorderColor="SpringGreen"  
                 />  
            </asp:Calendar>
        </ItemTemplate>
        </asp:DataList>
    </td>
    </tr>
    </table>

相应的ascx.cs文件是

    public partial class WebUserControl : System.Web.UI.UserControl
{
    int month = 1;
    public event EventHandler YearChanged;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadcalendar();

        }

    }
    private void loadcalendar()
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("Month", typeof(string));
        dt.Rows.Add("January");
        dt.Rows.Add("February");
        dt.Rows.Add("March");
        dt.Rows.Add("April");
        dt.Rows.Add("May");
        dt.Rows.Add("June");
        dt.Rows.Add("July");
        dt.Rows.Add("August");
        dt.Rows.Add("September");
        dt.Rows.Add("October");
        dt.Rows.Add("Novemeber");
        dt.Rows.Add("December");

        DataList1.DataSource = dt;
        DataList1.DataBind();

    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
              e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string year = ddlyear.SelectedValue.ToString();
            String str = ((Label)e.Item.FindControl("txt1")).Text;

            DateTime Now = DateTime.Now;
            DateTime TempDate = new DateTime(Convert.ToInt32(year), month, 1);

            // DateTime TempDate = new DateTime(Now.Year,Now.Month, 1);
            ((Calendar)e.Item.FindControl("Calendar1")).VisibleDate = TempDate;
            month = month + 1;
            //((Calendar)e.Item.FindControl("Calendar1")).SelectedDates.Clear();
        }

    }

    protected void ddlyear_SelectedIndexChanged(object sender, EventArgs e)
    {

            loadcalendar();
            YearChanged(sender, e);

    }

}

我需要在再次点击日期后取消选择日期。此代码将在页面中显示12个月的日历。我在一些论坛中读到我应该在calendar_selectedchange事件中提供我的代码。如何在我的代码中使用?如果还有其他方法可以使用以下代码取消选择日期吗?

2 个答案:

答案 0 :(得分:0)

我猜你要选择多个日期,所以

here's the solution(asp.net论坛)

here's the theory(msdn)

编辑(再次)

 <asp:Calendar ID="Calendar1" runat="server" onSelectionChanged="onSelectionChanged"
OnDayRender="onDayRender">

代码隐藏

    int month = 1;
    public event EventHandler YearChanged;

    //I've stored the selected date in the session.
    //There are others workaround, like saving it in the ViewState or in a db
    private DateTime yourDates
    {
        get
        {
            return (DateTime)Session["yourDates"];
        }
        set
        {
            Session["yourDates"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //always clear the session at start
            yourDates = new DateTime();
        }

    }

    protected void onSelectionChanged(object sender, EventArgs e)
    {
        DateTime selectedDate = (sender as Calendar).SelectedDate;

        if (yourDate == selectedDate)
        {
            yourDate = new DateTime();
            //it is vital to clear the selection to trigger this method again
            (sender as Calendar).SelectedDates.Clear();
        }
        else
        {
            yourDate = selectedDate;
            (sender as Calendar).SelectedDates.Clear();
        }
    }


    protected void onDayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date == yourDate)
        {
            e.Cell.BackColor = System.Drawing.Color.Green;
        }
        else
            e.Cell.BackColor = System.Drawing.Color.White;
    }

为每个日历单元调用onDayRender()方法。

答案 1 :(得分:0)

使用此代码有助于解决问题:

Calendar1.SelectedDate = Convert.ToDateTime("01/01/0001");