Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SharePoint Web Part error

Posted on 2011-03-07
3
Medium Priority
?
698 Views
Last Modified: 2012-05-11
Hello All,

I am trying to create a Web part that allows a user to enter a keyword query into a textbox, and display the query results in a grid..

My code below is deploys but when I run it I get the following error:

   
 
The "CustomSearchWebPart" Web Part appears to be causing a problem. Unable to correctly render SPGridView when it has no client ID. Ensure that the control has a parent.  Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings.

 

using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;


namespace CustomSearchWebPart
{
    [Guid("7080fbc8-2d83-43d9-acfd-177db83df946")]
    public class CustomSearchWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        SPGridView customGridView;
        SPDataSource customDataSource = new SPDataSource();

        Label lbl_Search = null;
        TextBox txt_Search = null;
        Button btn_Submit = null;

        public CustomSearchWebPart()
        {
        }

        void OnLoad(object sender, EventArgs e)
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            lbl_Search = new Label();
            txt_Search = new TextBox();

            lbl_Search.Text = "Search";
            Controls.Add(lbl_Search);

            txt_Search.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_Search);

            btn_Submit = new Button();
            btn_Submit.Width = new Unit(50, UnitType.Pixel);
            btn_Submit.Text = "Submit";
            btn_Submit.Click += new EventHandler(btn_Submit_Click);
            Controls.Add(btn_Submit);

            customGridView = new SPGridView();
            customGridView.Enabled = true;
            customGridView.AutoGenerateColumns = false;
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            SPSite site = new SPSite("http://vm142:8716/");
            ServerContext serverContext = ServerContext.GetContext(site);

            KeywordQuery query = new KeywordQuery(serverContext);

            query.QueryText = txt_Search.Text;

            ResultType resultType = ResultType.RelevantResults;
            query.ResultTypes = resultType;

            ResultTableCollection results = query.Execute();

            DataTable resultsTable = new DataTable();
            resultsTable.Load(results[resultType], LoadOption.OverwriteChanges);
            // apply data source here
            customGridView.DataSource = results;
            customGridView.DataBind();
        }

        protected override void Render(HtmlTextWriter writer)
        {

            writer.Write(@"<Table width='75%' align='center'><tr><td>");
            lbl_Search.RenderControl(writer);
            writer.Write("@</td><td>");
            txt_Search.RenderControl(writer);
            writer.Write("@</td></tr><tr><td colspan='2'>");
            btn_Submit.RenderControl(writer);
            writer.Write("@</td></tr><tr><td colspan='2'>");
            customGridView.RenderControl(writer);
            
        }
    }
}

Open in new window

0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:Pstry
ID: 35066964
Your code lacks assigning ID to the SPGridViewControl. In CreateChildControls method (line 57 or so) add a line:

customGridView.ID = "gridView"

Open in new window


This should help.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35076119
That did not work. Still get the same error
0
 
LVL 4

Accepted Solution

by:
Pstry earned 2000 total points
ID: 35081648
Hi TheInnovator,

I did check it again and I am sure about the reason. I have made your webpart as simply as posiible and here without assigning ID - it throws an error.

public class WebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
	SPGridView gridView;
	protected override void CreateChildControls()
	{
		gridView = new SPGridView();
		gridView.AutoGenerateColumns = false;
		gridView.ID = "id"; // Without this line does not work
		//this.Controls.Add(gridView); // alternate method that assigns ID
	}

	protected override void Render(System.Web.UI.HtmlTextWriter writer)
	{
		writer.Write("<div style='padding: 5px'>");
		this.gridView.RenderControl(writer);
		writer.Write("</div>");
	}
}

Open in new window


In fact, you have other problems with the code:

- you are not adding SPGridView to control collection (add: this.Controls.Add(customGridView) in the CreateChildControls)

- I suppose that gridview does not work without adding columns to it
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question