创建一个基本webpart
2024-07-21 02:23:27
供稿:网友
按照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);
}