Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Sharepoint Application , +
Hi, All
I am using SPGridView in custom web part, but It is giving error here base.Render(writer)- error is-- (Web Part appears to be causing a problem. Unable to cast object of type 'Microsoft.SharePoint.WebControls.MenuTemplate' to type 'System.Web.UI.WebControls.Table'.)
Even I commented All SPMenuField code but prob. is persist same.
protected override void Render(HtmlTextWriter writer)
        {
            using (SPSite _Site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb _Web = _Site.OpenWeb())
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            if (ChildControlsCreated)
                                spGridView.DataBind();
                            base.Render(writer);
                        });
                }
            }
 
<pre>
    }

Below is code example I am using.
spGridView and dataSource are my global object at class label.
protected override void CreateChildControls()
        {
 
<pre>
        dataSource = new ObjectDataSource();
        dataSource.ID = "dataSource";
        dataSource.SelectMethod = "GenerateTableTobindGrid";
        dataSource.TypeName = this.GetType().AssemblyQualifiedName;
        dataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(objectCreating);
        dataSource.Filtering += new ObjectDataSourceFilteringEventHandler(dataSource_Filtering);
        this.Controls.Add(dataSource);
 
        // SPGridView 
        spGridView = new SPGridView();
        spGridView.ID = "spGridView";
        spGridView.DataSourceID = dataSource.ID;
        spGridView.AutoGenerateColumns = false;
        spGridView.AllowPaging = true;
        spGridView.PageSize = 10;
        spGridView.AllowSorting = true;
        spGridView.EmptyDataText = string.Empty;
 
       SPMenuField nameCol1 = new SPMenuField();
        nameCol1.HeaderText = "Party";
        nameCol1.TextFields = "Party";
        nameCol1.NavigateUrlFields = "Party";
       nameCol1.SortExpression = "Party";
        nameCol1.MenuTemplateId = "menuTemplate";
 
       MenuTemplate menuTemplate = new MenuTemplate();
        menuTemplate.ID = "menuTemplate";
        this.Controls.Add(menuTemplate);
        spGridView.Columns.Add(nameCol1);
 
    MenuItemTemplate mitlUpdPaymt = new MenuItemTemplate("Open WebSite",         "~/_layouts/images/ASP16.GIF");
        //menuItemTemplate1.ClientOnClickNavigateUrl = "javascript:window.open('%KEYWEBSITE%');";
        mitlUpdPaymt.ClientOnClickNavigateUrl = "javascript:OpenPopUp();";
        menuTemplate.Controls.Add(mitlUpdPaymt);
 
      this.Controls.Add(spGridView);
      this.ChildControlsCreated = true;
   }

If any idea please assist me..
Thanks in Advance.

Arun Kaushik
Posted 29-Aug-12 20:32pm
Edited 29-Aug-12 20:37pm
(no name)500.5K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100