답변됨 Add custom webpart

  • jeudi 26 avril 2012 02:01
     
     

    Hi,

    I want to add custom webpart to a list without changing list properties.

    After I add custom webpart, I can't see List Tool bars and can't edit.

    Is there any way to add custom webpart without disappearing list ribbon?

    Thanks in advance!


    wendy


    • Modifié wendy.sh jeudi 26 avril 2012 04:41
    •  

Toutes les réponses

  • jeudi 26 avril 2012 07:15
     
     

    Adding the web part should not affect the ribbon , What is the web part you are adding exaclty ? Is it a custom or out of the box?

    If it is custom please copy paste code here to help you

  • vendredi 27 avril 2012 01:18
     
     

    Hi Ramez,

    I developed this webpart using visual studio 2010 and add to this list.

      public partial class Standard_ReportUserControl : UserControl
        {
            SPWeb spWeb = SPContext.Current.Web;
            
            string status = string.Empty;
            string businessUnit = string.Empty;
            string htmlTable = string.Empty;
            bool flag = false;
            public const string SELECT_ONE_TEXT = "Select One";

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Bind_gvDataView();
                    Bind_ddlBusinessUnit();               
                }
            }

            protected void btnView_Click(object sender, EventArgs e)
            {
                Bind_gvDataViewByFilter();
            }

            void Bind_ddlBusinessUnit()
            {
                SPList oList = spWeb.Lists["Business Unit"];
                SPListItemCollection collListItems = oList.Items;
                //SPUser currentUser = spWeb.CurrentUser;
                DataTable dt = new DataTable();

                dt = collListItems.GetDataTable();
                if (dt == null)
                {
                    ddlBusinessUnit.DataTextField = "Select One";
                }
                else
                {
                    if (dt.Rows.Count > 0)
                    {
                        ddlBusinessUnit.DataSource = dt;
                        ddlBusinessUnit.DataTextField = dt.Columns[2].ToString();
                        ddlBusinessUnit.DataValueField = dt.Columns[2].ToString();
                        ddlBusinessUnit.DataBind();

                        ddlBusinessUnit.Items.Insert(0, new ListItem(SELECT_ONE_TEXT));
                        ddlBusinessUnit.SelectedIndex = 0;
                    }
                }
            }
            
            void Bind_gvDataView()
            {
                 SPList spList = spWeb.Lists["WRA-OD Report Form"];            
                SPListItemCollection spListItem = spList.Items;
                //SPUser spUser = spWeb.CurrentUser;
                DataTable dt = new DataTable();
                dt = spListItem.GetDataTable();

                if (dt == null)
                {
                    lblMessage.Text = "No Record Found!";
                }
                else
                {
                    if (dt.Rows.Count > 0)
                    {
                        htmlTable += "<table width = 100%><tr><colgroup><col width=80px><col width=70px><col width=120px><col width=80px><col width=80px><col width=80px><col width=120px><col width=80px><col width=100px></colgroup><font color='black'><td>ID</td>" + "<td>Business Unit</td>" + "<td>Accident Date</td>" + "<td>Employee ID</td>" + "<td>Employee Name</td>" + "<td>Supervisor Name</td>" + "<td>Status</td>" + "<td>Created</td>" + "<td>Created On</td></font></tr>";
                        foreach (DataRow item in dt.Rows)
                        {
                            string strcreatedDate = item["Created"].ToString();
                            DateTime created = Convert.ToDateTime(strcreatedDate);
                            string createdString = created.ToShortDateString();
                            DateTime createdDate = DateTime.Parse(createdString);

                            DataTable dtTable = new DataTable();
                           
                            htmlTable += "<tr><td>" + item["Title"] + "</td><td>" + item["Business_x0020_Unit"] + "</td><td>" + item["When_x0020_did_x0020_the_x0020_a"] + "</td><td>" + item["Staff_x0020_ID_x0020_No_x002e_"] + "</td><td>" + item["Employee_x0020_Name"] + "</td><td>" + item["SupervisorAccountName"] + "</td><td>" + item["Status1"] + "</td><td>" + item["Author"] + "</td><td>" + item["Created"] + "</td></font></tr>";
                            lblMessage.Text = string.Empty;                       
                        }
                    }
                }
            }

            void Bind_gvDataViewByFilter()
            {
                SPList spList = spWeb.Lists["WRA-OD Report Form"];            
                SPListItemCollection spListItem = spList.Items;
                //SPUser spUser = spWeb.CurrentUser;
                DataTable dt = new DataTable();
                dt = spListItem.GetDataTable();

                if (dt == null)
                {
                    lblMessage.Text = "No Record Found!";
                    htmlTable = string.Empty;
                }
                else
                {
                    if (dt.Rows.Count > 0)
                    {
                        htmlTable += "<table width = 100%><tr><colgroup><col width=80px><col width=70px><col width=120px><col width=80px><col width=80px><col width=80px><col width=120px><col width=80px><col width=120px></colgroup><font color='black'><td>ID</td>" + "<td>Business Unit</td>" + "<td>Accident Date</td>" + "<td>Employee ID</td>" + "<td>Employee Name</td>" + "<td>Supervisor Name</td>" + "<td>Status</td>" + "<td>Created</td>" + "<td>Created On</td></font></tr>";
                        foreach (DataRow item in dt.Rows)
                        {
                            string strcreatedDate = item["Created"].ToString();
                            DateTime created = Convert.ToDateTime(strcreatedDate);
                            string createdString = created.ToShortDateString();
                            DateTime createdDate = DateTime.Parse(createdString);
                            string inputBusinessUnit = ddlBusinessUnit.SelectedValue;
                            string dbBusinessUnit = item["Business_x0020_Unit"].ToString();
                            //string smDBBusinessUnit = dbBusinessUnit.ToLower();
                            //string inputBusinessUnit = txtBusinessUnit.Text.ToLower();

                            string inputStatus = ddlStatus.SelectedValue;
                            string dbStatus = item["Status1"].ToString();

                              //if ((createdDate >= dtFromDate.SelectedDate) && (createdDate <= dtToDate.SelectedDate) || (dbBusinessUnit == inputBusinessUnit) ||(dbStatus == inputStatus))
                            //if ((createdDate >= dtFromDate.SelectedDate) && (createdDate <= dtToDate.SelectedDate) || (smDBBusinessUnit.Contains(inputBusinessUnit)) || (dbStatus == inputStatus))
                            if ((createdDate >= dtFromDate.SelectedDate) && (createdDate <= dtToDate.SelectedDate) || (dbStatus == inputStatus) || (dbBusinessUnit == inputBusinessUnit))
                                {
                                    DataTable dtTable = new DataTable();
                                    htmlTable += "<tr><td>" + item["Title"] + "</td><td>" + item["Business_x0020_Unit"] + "</td><td>" + item["When_x0020_did_x0020_the_x0020_a"] + "</td><td>" + item["Staff_x0020_ID_x0020_No_x002e_"] + "</td><td>" + item["Employee_x0020_Name"] + "</td><td>" + item["SupervisorAccountName"] + "</td><td>" + item["Status1"] + "</td><td>" + item["Author"] + "</td><td>" + item["Created"] + "</td></font></tr>";
                                    lblMessage.Text = string.Empty;
                                    flag = true;                                
                                }                       
                        }
                        ClearAll();
                        if (!flag)
                        {
                            lblMessage.Text = "No Record Found!";
                            htmlTable = string.Empty;
                        }
                    }
                }
            }
                 
         

            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write("<table width='80%'></br></br><tr ><td>");
                lblFromDate.RenderControl(writer);
                writer.Write("</td><td>");
                dtFromDate.RenderControl(writer);
                writer.Write("</td><td>");
                lblToDate.RenderControl(writer);
                writer.Write("</td><td colspan='2'>");
                dtToDate.RenderControl(writer);
                writer.Write("</td></tr><tr><td>");
                lblBusinessUnit.RenderControl(writer);
                writer.Write("</td><td>");
                ddlBusinessUnit.RenderControl(writer);
                //txtBusinessUnit.RenderControl(writer);
                writer.Write("</td><td>");
                lblStatus.RenderControl(writer);
                writer.Write("</td><td>");
                ddlStatus.RenderControl(writer);
                writer.Write("</td><td>");
                btnView.RenderControl(writer);       
                writer.Write("</td></tr><tr><td colspan='5'>");
                lblMessage.RenderControl(writer);
                writer.Write("</br></br></td></tr></table>");
                writer.Write(htmlTable);
            }     
        }


    wendy

  • vendredi 27 avril 2012 10:07
     
     

    Your code seams fine

    But I notices something that when I add two webpart to a list the ribbon will disappear until I select an item in the list is that the case for you too ?


    Ramez Al kara Software Consultant MCITP SHAREPOINT

  • lundi 30 avril 2012 02:04
     
     
    Yes Ramez.

    wendy

  • lundi 30 avril 2012 02:07
     
     Traitée

    When you have one web part on the page the ribbon will default to having that web part "selected" . With multiple web parts on the page you need to expicitly click on the list view web part to get the list ribbon menu.

    See here for discussion and solutions

    http://stackoverflow.com/questions/7992814/sharepoint-show-ribbon-with-multiple-webparts-on-page


    Please mark my response as an answer if appropriate.
    Learn.SharePoint.com

    • Marqué comme réponse Pengyu Zhao vendredi 4 mai 2012 05:50
    •