Solved

SharePoint Web Part error

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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