Solved

SharePoint Web Part error

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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…
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!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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