【问题标题】:How to add controls into a SharePoint WebPart?如何将控件添加到 SharePoint WebPart?
【发布时间】:2010-11-12 06:34:22
【问题描述】:

按照这个网站上的内容,我能够制作一个简单的 webpart http://www.codeguru.com/csharp/.net/net_asp/webforms/article.php/c12293/ 但现在我想添加控件,如 TextBoxes、Buttons、TreeViews ... 我怎样才能做到这一点?我编码的地方只是一个类库!如何使用设计器和页面进行编码?

【问题讨论】:

    标签: .net sharepoint


    【解决方案1】:

    尽量不要重写 WebPart 类的 Render 方法,而是像这样重写 CreateChildControls 方法:

    protected TextBox txtName;
    protected Button btnSubmit;
    
    // create child control
    protected override void CreateChildControls() {
        txtName = new TextBox();
        this.Controls.Add(txtName);
        btnSubmit = new Button();
        btnSubmit.Text = "Submit Name";
        this.Controls.Add(btnSubmit);
    }
    

    【讨论】:

    • 感谢您的回复。不幸的是,我试过了,但没有用。控件没有出现,但只有文本 hello world 出现了。我应该完全删除这个方法吗?
    • 您在哪里部署了 WebPart dll 。在广汽?更改 WebPart dll 后,您必须重新启动 IIS 才能使更改生效。
    【解决方案2】:

    您需要代码中的所有必需控件Controls.Add 在 CreateChildControls 方法中,您将无法像自定义 那样使用设计器来设计控件(直到 Visual Studio 2010 发布 - 它具有称为 Web 部件设计器的选项。请参阅此 link 以了解如何使用代码添加控件。如果要添加多个控件,排列控件和应用样式表将是一项艰巨的任务。我建议您使用SmartPart,它的作用是帮助您加载您创建为 webPart 的任何用户控件。因此,您无需担心使用代码添加控件、放置控件、设置样式。

    【讨论】:

    • +1 我同意,使用 ascx 用户控件并将其托管在您的 Web 部件中。
    • SmartPart 很棒。谢谢 :) 但现在我遇到了另一个问题,因为我在 Visual Studio 中看不到设计。有什么帮助吗? stackoverflow.com/questions/1158248/…
    【解决方案3】:

    希望这会有所帮助:

    Label lb1;
    protected override void CreateChildControls()
        {
            base.CreateChildControls(); 
            lb1 = new Label();
            lb1.ID = "label1";
            lb1.Text = "Controls in webpart";          
            this.Controls.Add(lb1);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 1970-01-01
      • 2011-06-23
      • 2012-11-23
      • 2013-09-24
      • 2010-12-23
      • 2015-12-12
      • 2012-02-14
      • 2011-02-19
      相关资源
      最近更新 更多