开发者

Create HTML table with rows and cells based on months and days using .Net

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 cl开发者_如何学Goient side code, etc.

JULY                  AUGUST    SEPTEMBER

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


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜