创建一个基本webpart

来源:互联网 发布:红外触摸屏校准软件 编辑:程序博客网 时间:2024/05/29 00:32

按照sps sdk的文档,我试着自己开发webpart,过程是这样的:

1、使用webpart模板创建一个工程

2、定义输出路径到interput/wwwroot/bin下。

3、设置版本号(每个.net程序都要做的)

4、准备好强名称所需的密钥,我给自己弄了一个zhouyikey.snk,放在c盘根目录下,所有的程序都要用它。

5、接下来就开始编辑代码了,首先检查自己所需的namespace是否都已经引用了

6、定义Toolbox Data,如:

[ToolboxData("<{0}:SimpleWebPart runat=server></{0}:SimpleWebPart>")]

7、定义XML namespace

[XmlRoot(Namespace="MyWebParts")]

我不知道所有的webpart都用默认的可不可以,但是,从文档来看,好像不太合适,因为XmlRoot这种方式使用全局的定义,我担心如果都使用默认的是不是就会有冲突。,所以,最好跟工程的namespace一直为好。

 

8、然后,就可以在renderWebPart方法中写自己要显示的东西了。

 

9、如果,你要在你的webpart上创建一些控件,你必须在renderWebPart中调用“RenderChildren(output);”,下面是创建控件的程序:

HtmlButton _mybutton;

        HtmlInputText _mytextbox;

        // Event handler for _mybutton control that sets the

        // Title property to the value in _mytextbox control.

        public void _mybutton_click (object sender, EventArgs e)

        {

            this.Title = _mytextbox.Value;

            try

            {

                this.SaveProperties=true;

            }

            catch

            {

                Caption = "Error... Could not save property.";

            }

        }

        // Override the ASP.NET Web.UI.Controls.CreateChildControls

        // method to create the objects for the Web Part's controls.     

        protected override void CreateChildControls ()

        {        

            // Create _mytextbox control.

            _mytextbox = new HtmlInputText();

            _mytextbox.Value="";

            Controls.Add(_mytextbox);

            // Create _mybutton control and wire its event handler.

            _mybutton = new HtmlButton();

            _mybutton.InnerText = "Set Web Part Title";

            _mybutton.ServerClick += new EventHandler (_mybutton_click);

            Controls.Add (_mybutton);

        }

 

 

 

原创粉丝点击