使用 .Net 根据月份和日期创建包含行和单元格的 HTML 表格

发布于 2024-11-17 17:45:58 字数 288 浏览 1 评论 0原文

如何根据月份和天数动态创建表格?例如,我想在一行上显示滚动的 3 个月,然后在该月下方的第二行上显示星期日,如下所示。不确定我是否应该使用服务器端代码或客户端代码等。

七月                八月   九月
3 10 17 24 31   7 14 21 28   4 11 18 25

How do I dynamically create a table based on months and number of days? For example, I want to display a rolling 3 months on one row and then show the Sunday day on the second row underneath the month like below. Not sure if I should use server side code or client side code, etc.

JULY                  AUGUST    SEPTEMBER
3 10 17 24 31    7 14 21 28    4 11 18 25

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-11-24 17:45:58

使用服务器端代码或客户端代码取决于您的项目状态,因此请参阅我的示例服务器端代码

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    List<Int32> Months = new List<Int32>() { 7, 8, 9 };
    if (!IsPostBack)
    {
        gv.DataSource = Months;
        gv.DataBind();
    }
}
protected void gv_DataBinding(Object sender, RepeaterItemEventArgs  e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal ltrMonth = e.Item.FindControl("ltrMonth") as Literal;
        DateTime _date = new DateTime(DateTime.Now.Year, (Int32)e.Item.DataItem, 1); 
        ltrMonth.Text = _date.ToString("MMMM");
         DateTime temp;
         List<Int32> sundays=new List<Int32>();
         for(var i=1;i<32;i++)
         {
             if(DateTime.TryParse(_date.Year+"/"+_date.Month+"/"+i,out temp))
             {
                 if(temp.DayOfWeek==DayOfWeek.Sunday)
                 {
                     sundays.Add(i);
                 }
             }
         }


        Repeater rptrSunday = e.Item.FindControl("rptrSunday") as Repeater;
        rptrSunday.DataSource = sundays;
        rptrSunday.DataBind();
    }
}
protected void rptrSunday_DataBinding(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal ltrDay = e.Item.FindControl("ltrDay") as Literal;
        ltrDay.Text = e.Item.DataItem.ToString();
    }
}

ASPX

<asp:Repeater runat="server" ID="gv" OnItemDataBound="gv_DataBinding">
    <HeaderTemplate>
        <table>
        <tr>
    </HeaderTemplate>
    <ItemTemplate>
            <td>
                <asp:Literal runat="server" ID="ltrMonth"></asp:Literal>
                <table>
        <tr>
            <td>
                <asp:Repeater runat="server" ID="rptrSunday" OnItemDataBound="rptrSunday_DataBinding">
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="ltrDay"></asp:Literal></ItemTemplate>
                </asp:Repeater>
            </td>
        </tr></table>
            </td>
    </ItemTemplate>
    <FooterTemplate>
        </tr>
        </table></FooterTemplate>
</asp:Repeater>

using server side code or client side code depends on your project status so see my sample server side code

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    List<Int32> Months = new List<Int32>() { 7, 8, 9 };
    if (!IsPostBack)
    {
        gv.DataSource = Months;
        gv.DataBind();
    }
}
protected void gv_DataBinding(Object sender, RepeaterItemEventArgs  e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal ltrMonth = e.Item.FindControl("ltrMonth") as Literal;
        DateTime _date = new DateTime(DateTime.Now.Year, (Int32)e.Item.DataItem, 1); 
        ltrMonth.Text = _date.ToString("MMMM");
         DateTime temp;
         List<Int32> sundays=new List<Int32>();
         for(var i=1;i<32;i++)
         {
             if(DateTime.TryParse(_date.Year+"/"+_date.Month+"/"+i,out temp))
             {
                 if(temp.DayOfWeek==DayOfWeek.Sunday)
                 {
                     sundays.Add(i);
                 }
             }
         }


        Repeater rptrSunday = e.Item.FindControl("rptrSunday") as Repeater;
        rptrSunday.DataSource = sundays;
        rptrSunday.DataBind();
    }
}
protected void rptrSunday_DataBinding(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal ltrDay = e.Item.FindControl("ltrDay") as Literal;
        ltrDay.Text = e.Item.DataItem.ToString();
    }
}

ASPX

<asp:Repeater runat="server" ID="gv" OnItemDataBound="gv_DataBinding">
    <HeaderTemplate>
        <table>
        <tr>
    </HeaderTemplate>
    <ItemTemplate>
            <td>
                <asp:Literal runat="server" ID="ltrMonth"></asp:Literal>
                <table>
        <tr>
            <td>
                <asp:Repeater runat="server" ID="rptrSunday" OnItemDataBound="rptrSunday_DataBinding">
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="ltrDay"></asp:Literal></ItemTemplate>
                </asp:Repeater>
            </td>
        </tr></table>
            </td>
    </ItemTemplate>
    <FooterTemplate>
        </tr>
        </table></FooterTemplate>
</asp:Repeater>
蓝眸 2024-11-24 17:45:58

只需使用 3 个日历控件设置您的 3 个不同月份。

Just use 3 calendar controls set to your 3 different months.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文