Solved

SharePoint Web Part error

Posted on 2011-03-07
3
695 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 500 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

623 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