1

我有一个文本框和一个按钮。当我在文本框中输入月份和年份(例如:2013 年 4 月)并单击按钮时,应显示该特定月份的自定义日历。

注意:日历中没有星期六或星期日。日子只会从星期一到星期五。

它应该是使用 C# 的基于 Web 的 ASP.NET 应用程序。

我怎样才能做这个定制的日历?提供实现上述功能的示例代码。

4

1 回答 1

6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

  protected void Button1_Click(object sender, EventArgs e)
  {
    string month = txtMonth.Text;// should be in the format of Jan, Feb, Mar, Apr, etc...
    int yearofMonth = Convert.ToInt32(txtYear.Text);
    DateTime dateTime = Convert.ToDateTime("01-" + month + "-" + yearofMonth);
    DataRow dr;
    DataTable dt = new DataTable();
    dt.Columns.Add("Monday");
    dt.Columns.Add("Tuesday");
    dt.Columns.Add("Wednesday");
    dt.Columns.Add("Thursday");
    dt.Columns.Add("Friday");
    dr = dt.NewRow();
    for (int i = 0; i < DateTime.DaysInMonth(dateTime.Year, dateTime.Month); i += 1)
    {
        txtMonth.Text = Convert.ToDateTime(dateTime.AddDays(0)).ToString("dddd");
        if (Convert.ToDateTime(dateTime.AddDays(i)).ToString("dddd") == "Monday")
        {
            dr["Monday"] = i + 1;
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Tuesday")
        {
            dr["Tuesday"] = i + 1;
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Wednesday")
        {
            dr["Wednesday"] = i + 1;

        }
        if (dateTime.AddDays(i).ToString("dddd") == "Thursday")
        {
            dr["Thursday"] = i + 1;
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Friday")
        {
            dr["Friday"] = i + 1;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            continue;
        }
        if (i == DateTime.DaysInMonth(dateTime.Year, dateTime.Month) - 1)
        {
            dt.Rows.Add(dr);
            dr = dt.NewRow();

        }

    }

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

  }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="txtMonth" runat="server">Mon</asp:TextBox>
        <asp:TextBox ID="txtYear" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

    </div>
    </form>
</body>
</html>

谢谢...

于 2013-02-25T10:58:55.673 回答