[Umbraco] Data Type之Render control

继续探讨Data Type。如果你创建过Data Type,你就会知道创建一个新的Data Type都需要指定一个Render control,这有点类似开始C#时用到的继承。

那么如何创建我们自己的Render control呢?

[Umbraco] Data Type之Render control

其实也很简单,将前一篇的demo略加改造就可以了

 1 public class ConnectionStringDataType : AbstractDataEditor

 2 {

 3  [DataEditorSetting("Connectionstring")]

 4  public String ConnectionString { get; set; }

 5 

 6  [DataEditorSetting("Select statement")]

 7  public string SelectStatement { get; set; }

 8 

 9  [DataEditorSetting("Text column")]

10  public string TextColumn { get; set; }

11 

12  [DataEditorSetting("Value column")]

13  public string ValueColumn { get; set; }

14 

15  private DropDownList control = new DropDownList();

16 

17  public ConnectionStringDataType()

18  {

19   base.RenderControl = control;

20   control.Init += new EventHandler(control_Init);

21   base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(DataEditorControl_OnSave);

22  }

23 

24  void DataEditorControl_OnSave(EventArgs e)

25  {

26   base.Data.Value = control.SelectedValue;

27  }

28 

29  void control_Init(object sender, EventArgs e)

30  {

31   control.DataSource = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, SelectStatement);

32   control.DataTextField = TextColumn;

33   control.DataValueField = ValueColumn;

34   control.DataBind();

35   control.Items.Insert(0, new ListItem(String.Empty, String.Empty));   control.SelectedIndex = 0;

36 

37   if (base.Data.Value != null)

38    control.SelectedValue = base.Data.Value.ToString();

39  }

40 

41  public override Guid Id

42  {

43   get { return new Guid("FEDBD0B7-F318-4F1A-8D37-97C3B7DCB5D8"); }

44  }

45 

46  public override string DataTypeName

47  {

48   get { return "Db driven dropdown"; }

49  }

50 }
View Code

 

你可能感兴趣的:(type)