Solved

SharePoint Web Part error

Posted on 2011-03-07
3
684 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
  • 2
3 Comments
 
LVL 4

Expert Comment

by:Pstry
Comment Utility
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
Comment Utility
That did not work. Still get the same error
0
 
LVL 4

Accepted Solution

by:
Pstry earned 500 total points
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now