DataList实现日程表

来源:互联网 发布:java中abstract的用法 编辑:程序博客网 时间:2024/05/17 06:41

前台:DataList 

<asp:DataList ID="dl_Calendar" Width="500px" runat="server" BorderColor="#99CCFF" BorderWidth="1px"
                CellPadding="1" GridLines="Both" RepeatColumns="7" RepeatDirection="Horizontal" OnItemDataBound="dl_Calendar_ItemDataBound" OnEditCommand="dl_Calendar_EditCommand" OnUpdateCommand="dl_Calendar_UpdateCommand" >
                <HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Size="14px"
                    Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
                <HeaderTemplate>
                    <table style="width: 490px">
                        <tr>
                            <td style="width: 70px">
                <asp:LinkButton ID="btn_Edit" runat="server" CssClass="Edit" CommandName="Pre" OnCommand="dl_Command" Text="上个月"></asp:LinkButton></td>
                <td colspan="5" style="width: 350px">
                <asp:Label runat="server" ID="lab_Time"></asp:Label></td>
                <td style="width: 70px">
               <asp:LinkButton ID="LinkButton1" runat="server" CssClass="Edit" CommandName="Next" OnCommand="dl_Command" Text="下个月"></asp:LinkButton></td>
                        </tr>
                        <tr>
                            <td>
                                日</td>
                            <td>
                                一</td>
                            <td>
                                二</td>
                            <td>
                                三</td>
                            <td>
                                四</td>
                            <td>
                                五</td>
                            <td>
                                六</td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                </ItemTemplate>
                <EditItemTemplate>
                </EditItemTemplate>
                <AlternatingItemStyle Width="70px" />
                <ItemStyle Width="70px" />
            </asp:DataList>

后台:

 System.Globalization.Calendar calender = System.Globalization.CultureInfo.InvariantCulture.Calendar;
    private int year //定义年的变量的初始值
    {
        get
        {
            if (ViewState [ "Year" ]!=null )
               
            {
                if ( int.Parse ( ViewState [ "Year" ].ToString ( ) ) > 9999 && int.Parse ( ViewState [ "Year" ].ToString ( ) ) < 1000 )
                {
                    return DateTime.Now.Year;
                }
                else
                {
                    return int.Parse ( ViewState [ "Year" ].ToString ( ) );
                }
               
            }
            else
            {
                return DateTime.Now.Year;
            }
        }
        set
        {
            ViewState [ "Year" ] = value;
        }
    }
    private int month       //定义月的变量的初始值
    {
        get
        {
            if ( ViewState [ "Month" ]!=null )
             {
                 if ( int.Parse ( ViewState [ "Month" ].ToString ( ) ) < 0 && int.Parse ( ViewState [ "Month" ].ToString ( ) ) >= 13 )
                 {
                        return DateTime.Now.Month;
                 }
                 else
                 {
                     return int.Parse ( ViewState [ "Month" ].ToString ( ) );
                 }
            }
            else
            {
                return DateTime.Now.Month;
            }
        }
        set
        {
            ViewState [ "Month" ] = value;
        }
    }
    private int day           //定义日的变量的初始值
   {
       get
       {
           if ( ViewState [ "Day" ] != null )
           {
               if ( int.Parse ( ViewState [ "Day" ].ToString ( ) ) < 0 && int.Parse ( ViewState [ "Day" ].ToString ( ) ) > 31 )
               {
                   return DateTime.Now.Day;
               }
               else
               {
                   return int.Parse ( ViewState [ "Day" ].ToString ( ) );
               }
           }
           else
           {
               return DateTime.Now.Day;
           }
       }
        set
        {
            ViewState [ "Day" ] = value;
        }
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        dl_DataBind ( );
    }

    private void dl_DataBind ( )
    {

        DateTime dt = new DateTime ( year, month, 1 );
        int w =  (int)dt.DayOfWeek;

        int daysInMonth = calender.GetDaysInMonth ( year, month );
        ArrayList date = new ArrayList ();          //定义日历展示用的数组
        for ( int i = 0;i < w;i++ )
        {
            date.Add ( "" );
        }
        for ( int i = 0;i < daysInMonth;i++ )
            date.Add( i + 1 );
      
        dl_Calendar.DataSource = date;
        dl_Calendar.DataBind ( );
    }
    protected void dl_Command ( object sender, CommandEventArgs e )
    {
        switch ( e.CommandName )
        {
            case "Pre":
                if ( month == 1 )
                {
                    year = year - 1;
                    month = calender.GetMonthsInYear(year);
                }
                else
                {
                    month = month - 1;
                }
                dl_Calendar.EditItemIndex = -1;
                dl_DataBind ( );
                break;

            case "Next":
                if ( month == calender.GetMonthsInYear(year))
                {
                    year = year + 1;
                    month = 1;
                }
                else
                {
                    month = month + 1;
                }
                dl_Calendar.EditItemIndex = -1;
                dl_DataBind ( );
                break;
        }
    }
    protected void dl_Calendar_ItemDataBound ( object sender, DataListItemEventArgs e )
    {
        if ( e.Item.ItemType == ListItemType.Header)
        {
            Label lab_Time = (Label) e.Item.FindControl ( "lab_Time" );
            lab_Time.Text = year + "-" + month + "-" + day;
        }
        if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)
        {
            if ( e.Item.DataItem.ToString() != "" )
            {
               
                Button btn_edit = new Button ( );
               
                btn_edit.BorderStyle = BorderStyle.None;
                btn_edit.Text = e.Item.DataItem.ToString();
                btn_edit.BackColor = System.Drawing.Color.White;
                btn_edit.BorderColor = System.Drawing.Color.White;
                btn_edit.CommandArgument = e.Item.DataItem.ToString ( );
                btn_edit.CommandName = "Edit";
                e.Item.Controls.Add( btn_edit );
                if ( Context.User.Identity.IsAuthenticated )
                {
                    DateTime dt = new DateTime ( year, month, day );
                    IList ilist = ( IList ) Trisence.BLL.SNS.Calendar.ListByMonth ( ( ( TIdentity ) Context.User.Identity ).ID, dt );
                    foreach ( Trisence.BLL.SNS.Calendar c in ilist )
                    {
                        if ( c.PlanDate.Day == int.Parse ( e.Item.DataItem.ToString ( ) ) && c.PlanDate.Month ==month && c.PlanDate.Year ==year)
                        {
                            Label lab = new Label ( );
                            lab.Text = c.Arranging;
                            lab.Width = Unit.Pixel ( 65 );
                            lab.ForeColor = System.Drawing.Color.Red;
                            e.Item.Controls.Add ( lab );
                        }
                    }
                }
            }
        }
        if ( e.Item.ItemType == ListItemType.EditItem )
        {
            TextBox tb = new TextBox ( );
            tb.ID = "tb";
            tb.Width = Unit.Pixel ( 65 );
            tb.Height = Unit.Pixel ( 75 );
            tb.TextMode = TextBoxMode.MultiLine;
            tb.MaxLength = 200;

            Button btn_Up = new Button ( );
            btn_Up.Text = "提交";
            btn_Up.CommandArgument = e.Item.DataItem.ToString ( );
            btn_Up.CommandName = "Update";
            e.Item.Controls.Add ( tb );
            e.Item.Controls.Add ( btn_Up );
        }
       
    }
    protected void dl_Calendar_EditCommand ( object source, DataListCommandEventArgs e )
    {
                   dl_Calendar.EditItemIndex = e.Item.ItemIndex;
                dl_Calendar.DataBind ( );
     }
       protected void dl_Calendar_UpdateCommand ( object source, DataListCommandEventArgs e )
    {
       
       TextBox tb = (TextBox)e.Item.FindControl ( "tb" );

        string str = tb.Text.Trim ( );
        if ( 验证用户输入 )
        {
            Builder.ThrowAlertMessage ( this.Page, "输入不能为空,或含有非法字符!请重新填写!" );
            return;
        }
        int d = 1;

        try
        {
             d = int.Parse ( e.CommandArgument.ToString ( ) );
        }
        catch
        {
            Builder.ThrowAlertMessage ( this.Page, "错误!" );
            return;
        }
        DateTime dt = new DateTime ( year, month, d );


        if ( 更新数据库)
        {
            dl_Calendar.EditItemIndex = -1;
            dl_Calendar.DataBind ( );
        }
        else
        {
            Builder.ThrowAlertMessage ( this.Page, "更新数据库错误!" );
            return;
        }
    }

页面可浏览 www.sigbang.com(需注册)