I have embedded a WebGrid into a SharePoint web part and it works OK - I create the grid programatically and add it using CreateChildControls method.
I have one issue which is after I add my WegGrid-containing web part to the MOSS page. I do this in MOSS via Site Actions - Edit Page and edit the web part page, click a web part zone and pick my web part to add it. It goes onto the page fine, but I can't close the web part using the X in the top right corner. I can add other built-in web parts to the page and close them OK, e.g. content query web part, content editor web part etc. But when I try to close my own web part in MOSS the browser doesn't post back and just does nothing. The same thing happens if I click the Edit dropdown and select Close or Delete - again the web part is not removed from the page.
I know this isn't a MOSS forum but I wonder if anyone else has had this problem? Seems like it might be to do with Javascript on the page?
The browser is IE7. Firefox doesn't have the same issue but you need to use IE really for MOSS work.
I get the same issue even with a very simple grid as per the example below. It seems to jam up the postback on the page so none fo the SharePoint buttons will do a postback. If I comment out the call to Bind() then it works OK. Is there something else I need to do?
using System; using System.Collections.Generic; using System.Text; using Infragistics.WebUI.UltraWebGrid; using System.Data; namespace UltraGridDemoWP2 { public class UltraGridDemoWP2 : System.Web.UI.WebControls.WebParts.WebPart { private UltraWebGrid _grid; public UltraWebGrid Grid { get { EnsureChildControls(); return _grid; } } protected override void CreateChildControls() { base.CreateChildControls(); _grid = new UltraWebGrid("Grid"); this.Controls.Add(_grid); Bind(); // Comment this out and it doesn't jam up the postback } private void Bind() { Grid.DataSource = getdata(); Grid.DataBind(); } private DataTable getdata() { DataTable t = new DataTable(); t.Columns.Add(new DataColumn("Col1")); t.Columns.Add(new DataColumn("Col2")); for (int i = 0; i < 10; i++) { DataRow r = t.NewRow(); r["Col1"] = "col1-" + i.ToString(); r["Col2"] = "col2-" + i.ToString(); t.Rows.Add(r); } return t; } } }
OK I have stumbled on a solution but I'm not sure exactly why it works! I found if you set the grid to have an ID that is a GUID, everything works OK. So I changed the grid variable to intialise it:
private UltraWebGrid _grid = null;
protected override void CreateChildControls() { base.CreateChildControls(); if (_grid == null) { _grid = new UltraWebGrid(Guid.NewGuid().ToString()); } this.Controls.Add(_grid); Bind(); }
My thinking was that there might be a problem with invisible closed webgrids on the page and that I had to make sure that each webgrid had a unique ID on the page, so I gave it a GUID. However, I have tried replacing the Guid.NewGuid().ToString() with a very large random number generated from the Random class and that still causes the error even though the random ID of the grid is unique on the page. It just seems like the ID has to be a GUID string - can anyone explain?
Another workaround is to set the ReadOnly property of the webgrid within the CreateChildControls method - this reduces the amount of Javascript that the control renders onto the page:
Grid.DisplayLayout.ReadOnly = ReadOnly.LevelTwo;
I think I recall us encountering this problem (or something similar) when building our Sharepoint reference application (http://sharepoint.infragistics.com/). I forwarded your message to the team that build it to see if they can shed some light on it.
What version of NetAdvantage are you using?
Devin
OK thanks - I'm using NetAdvantage 2008 CLR. 2.0
I think this could be a combined INamingContainer / javascript variable naming issue. I have just tried your code (thanks for sharing) and it appears that using numbers as IDs is not valid in javascript - the variable name must start with letter, "_", etc, so if you are not appending this to a control that implements INamingContainer (and has a valid explicitly set ID), you will get invalid javascript ID handles and errors.
Guids are also problematic sometimes, because their parts are delimited with "-" which is again not valid (I am not completely sure why you did not get error there). I believe the safest way to go is something like this:
_grid = new UltraWebGrid(); _grid.ID = "WebGrid" + System.Guid.NewGuid().ToString().Replace("-", "_");
This will guarantee uniquess, plus the handle of the javascript client ID will be correct even if you are not adding your WebPart to a control implementing INamingContainer. I have just tried the following with good results
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Guid u = System.Guid.NewGuid(); PlaceHolder1.Controls.Add(new UltraGridDemoWP2()); PlaceHolder1.Controls.Add(new UltraGridDemoWP2()); PlaceHolder1.Controls.Add(new UltraGridDemoWP2()); } public class UltraGridDemoWP2 : System.Web.UI.WebControls.WebParts.WebPart { private UltraWebGrid _grid; public UltraWebGrid Grid { get { EnsureChildControls(); return _grid; } } protected override void CreateChildControls() { base.CreateChildControls(); _grid = new UltraWebGrid(); _grid.ID = "WebGrid" + System.Guid.NewGuid().ToString().Replace("-", "_"); this.Controls.Add(_grid); Bind(); // Comment this out and it doesn't jam up the postback } private void Bind() { Grid.DataSource = getdata(); Grid.DataBind(); } private DataTable getdata() { DataTable t = new DataTable(); t.Columns.Add(new DataColumn("Col1")); t.Columns.Add(new DataColumn("Col2")); for (int i = 0; i < 10; i++) { DataRow r = t.NewRow(); r["Col1"] = "col1-" + i.ToString(); r["Col2"] = "col2-" + i.ToString(); t.Rows.Add(r); } return t; } }
Thanks for sharing the solution Lars - I've updated the "Known Issues" forum with your findings - surely will be useful to a lot of people.
Thanks again.
Ok I installed NetAdvantage 2008 Volume 2 and that has finally fixed the problem. Volume 2 installs as a separate product from Volume 1 and I upgraded my project to use the Volume 2 controls using the project upgrade utility. I didn't change any of my code, and I do not explicitly set the ID of the webgrid in code. By referencing volume 2 version of the webgrid it now closes correctly when embedded into a webpart.
Thanks for your help and advice
Thanks Todd. I have MOSS SP1. I have downloaded volume 2 as you suggested and installed it but it doesn't seem to go over the top of Volume 1 - Volume 2 pops up as a separate product. Should I uninstall Vol 1 first and then install Vol 2?
Quick followup. Make sure you have SP1 installed for SharePoint (MOSS). The service pack include support for running AJAX based web parts and includes a number of JavaScript related fixes.
Let us know if this works for you.
Direct Link to the sample: http://users.infragistics.com/tsnyder/SharePointAJAX/Sample-AJAX-Grid.zip