創建一個基本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);
}