'ListViewWebPart'是'命名空间',但用作'类型'

时间:2010-10-25 15:20:58

标签: c# namespaces

嗨,我收到错误'ListViewWebPart'是'命名空间',但用作“类型”

这是我的代码,我不知道如何修复这个想法?

namespace TestSolution
{

    [Guid("d3425822-6979-476d-a8cb-04de9521d1b4")]
    public class TestListViewWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {


        public TestListViewWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }



        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                SPSite site = new SPSite("http://test.local/docs");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["ListName"];



                ViewToolBar toolbar = new ViewToolBar();

                SPContext context = SPContext.GetContext(this.Context, list.Views["SomeView"].ID, list.ID, SPContext.Current.Web);

                toolbar.RenderContext = context;

                Controls.Add(toolbar);



                // Instantiate the web part
                ListViewWebPart lvwp = new ListViewWebPart();
                lvwp.ListName = list.ID.ToString("B").ToUpper();
                lvwp.ViewGuid = list.Views["SomeView"].ID.ToString("B").ToUpper();
                //lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

                lvwp.GetDesignTimeHtml();
                this.Controls.Add(lvwp);
            }
            catch (Exception ex)
            {
                Label lbl = new Label();
                lbl.Text = "Error occured: ";
                lbl.Text += ex.Message;
                this.Controls.Add(lbl);
            }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();
            base.Render(writer);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在应用程序的某处,您声明了一个名称空间ListViewWebPart。在您的代码中,您现在还声明了一个具有相同名称的变量,这是不允许的。一般来说,你永远不应该有一个具有相同名称的类和命名空间,这通常会导致问题。

如果要避免重命名,则必须为变量指定完整的命名空间,如下所示:

YourNamespace.ListViewWebPart lvwp = new YourNamespace.ListViewWebPart();

甚至更好:

global::YourNamespace.ListViewWebPart lvwp = new global::YourNamespace.ListViewWebPart();