写webpart

1. 在webpart中添加控件
    举例说明:
    a> 首先声明控件: LinkButton btnFirst;
    b> 添加代码: 在函数protected override void CreateChildControls()里添加如下代码
            btnFirst = new LinkButton();
            btnFirst.Text = "|<";
            btnFirst.Click += new EventHandler(btnFirst_Click);
            this.Controls.Add(btnFirst);

    c> 再添加控件事件:
        /// <summary>
        /// First Button Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFirst_Click(object sender, EventArgs e)
        {
            \\\\\\\\\\
        }
    d> 最后在需要的位置添加控件
        output.Write("<td width=\"130\" align=\"left\">");
        btnFirst.RenderControl(output);
        output.Write("</td >");


        webpart里函数的执行顺序一般是先CreateChildControls(),再
        protected override void RenderWebPart(HtmlTextWriter xxx)

2. 通过URL获取List(or Document Library)
    SPSite site = new SPSite(SPContext.Current.Site.ID);
    SPList myList;
    try
        {
             myList = site.RootWeb.GetList(Url);
        }
    catch
        {
        }
        
    SPListItemCollection items = myList.Items;

3. 获取list(or Document Library)内容到DataTable
     DataTable GetData()
        {
            DataTable DT = new DataTable();
            DT.Columns.Add("Created", Type.GetType("System.DateTime"));
            DT.Columns.Add("Owner", Type.GetType("System.String"));
            DT.Columns.Add("Business Unit", Type.GetType("System.String"));
            DT.Columns.Add("Health", Type.GetType("System.String"));
            DT.Columns.Add("End Date", Type.GetType("System.String"));
            DT.Columns.Add("Project Name", Type.GetType("System.String"));
            DT.Columns.Add("Url", Type.GetType("System.String"));

            foreach (SPListItem listItem in items)
            {
                DataRow DR = DT.NewRow();
                DR["Created"] = listItem["Created"].ToString();
                DR["Owner"] = listItem["Owner"].ToString();
                DR["Business Unit"] = listItem["Business Unit"].ToString();
                DR["Health"] = listItem["Health"].ToString();
                DR["End Date"] = listItem["End Date"].ToString();
                DR["Project Name"] = listItem["Project Name"].ToString();
                DR["URL"] = Url + "/" + "DispForm.aspx?ID=" + listItem.ID;
            }
posted @ 2008-05-26 10:51  ChoiceShan  阅读(205)  评论(0编辑  收藏  举报