WebPart Complex Property

下面代码是实现一个Dropdown,同时实现其同步变化。代码如下:

Complex Property
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;
using System.Collections;

namespace WebPartCollection
{
[Guid("cf18e6af-3ab5-4503-8a41-c24ac8d38566")]
public class ComplexProperty : System.Web.UI.WebControls.WebParts.WebPart
{
public ComplexProperty()
{
}
private string _FirstName;
[Personalizable]
[WebBrowsable(true)]
[Category("Name")]
[WebDisplayName("FirstName")]
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;
[Personalizable]
[WebBrowsable(true)]
[Category("Name")]
[WebDisplayName("LastName")]
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
public override EditorPartCollection CreateEditorParts()
{
ArrayList editorArray = new ArrayList();

WebPartProperty1EditPart edPart = new WebPartProperty1EditPart();
edPart.ID = "WebPartProperty1EditPart1";
editorArray.Add(edPart);

EditorPartCollection initEditorParts = base.CreateEditorParts();

EditorPartCollection editorParts = new EditorPartCollection(initEditorParts, editorArray);

return editorParts;
}
protected override void CreateChildControls()
{

base.CreateChildControls();

}
}

public class WebPartProperty1EditPart : EditorPart
{
private DropDownList _dlValueList;
private Label _lbCompany;


//创建下拉列表控件
protected override void CreateChildControls()
{
_lbCompany = new Label();
_lbCompany.Text = "Company Name: ";

_dlValueList = new DropDownList();
_dlValueList.Items.Add("Carat");
_dlValueList.Items.Add("HR");

this.Controls.Add(_lbCompany);
this.Controls.Add(new LiteralControl("<br/>"));
this.Controls.Add(_dlValueList);
}
//将WebPart的值同步到下拉控件
public override void SyncChanges()
{
this.EnsureChildControls();
ComplexProperty wp = (ComplexProperty)this.WebPartToEdit;
_dlValueList.SelectedValue = wp.FirstName;
}
//将下拉控件选择的值应用到WebPart
public override bool ApplyChanges()
{
this.EnsureChildControls();
ComplexProperty wp = (ComplexProperty)this.WebPartToEdit;
wp.FirstName = _dlValueList.SelectedValue;
return true;
}
}

}



 

你可能感兴趣的:(property)