View Article        

Current Articles | Categories | Search | Syndication

Page: 3 of 3
Previous Page | Next Page
posted @ Monday, May 12, 2008 10:07 PM by SkySigal
And what about DataBinding?

Technically, DataBinding goes a little bit beyond the bounds of a discussion about Delegating CompositeControl properties -- but it's worth touching upon.

For each child control of the composite control that is going to be databound, you will need to provide the basic DataBind fields, each one being Delay Delegated:

[PersistenceMode(PersistenceMode.Attribute)]
public string DataSourceID_Categories {
    get {object o=ViewState["DataSourceID_Categories"]; return (o!=null)?(string)o:string.Empty;}
    set {ViewState["DataSourceID_Categories"]=value;}
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object DataSource_Categories {
    get { return ViewState["DataSource_Categories"];}
    set { ViewState["DataSource_Categories"]=value; }
}

[PersistenceMode(PersistenceMode.Attribute)]
public string DataMember_Categories {
    get { object o=ViewState["DataMember_Categories"]; return (o!=null)?(string)o:string.Empty; }
    set { ViewState["DataMember_Categories"]=value; }
}
ddition you most probably will want to know what the user is currently selecting: 
[PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ListItemCollection Categories {
    get {EnsureChildControls(); return ContainerMain.ListControl_Categories.Items;}
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Category {
    get {EnsureChildControls();return _ListControlCategory.Text;}
    set {EnsureChildControls();_ListControlCategory.Text = value;}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int CategoryIndex {
    get {EnsureChildControls();return _ListControlCategory.SelectedIndex;}
    set {EnsureChildControls();_ListControlCategory.SelectedIndex = value;}
}

 

...Attributes:
Notice how the Attributes have been applied:

  1. Categorieshave been marked to save the Contents (ie the Items) of the ListItemCollectionas inner tags
  2. Categoryhas been marked to not serialize its value as an attribute -- only be workable run-time.
  3. CategoryIndexhas been marked to not serialize its value as an attribute -- only be workable run-time.

 

Transfering early, in CreateChildControls rather than PrepareControlHierarchy

The question is when to transfer these properties to the inner Control, and the answer is that it cannot be done as late as PrepareControlHierarchy, but must be done in CreateChildControls:
TODO:Explain Why?

protected void CreateChildControls(){
    ...
    listControl = this.ContainerMain.ListControl_Categories;
    ...
    listControl.DataSourceID = this.DataSourceID_Categories;
    listControl.DataSource = this.DataSource_Categories;
    listControl.DataMember = this.DataMember_Categories;
    listControl.DataTextField = this.DataBindingFieldNames.CategoryTextField;
    listControl.DataValueField = this.DataBindingFieldNames.CategoryValueField;
    ...
}

 

 

Now -- in ASP.NET 2.0, if you are going to be databinding, you're going to be enheriting your compositecontrol from Common.BaseDataBoundControl-- which means you will be overriding PerformDataBinding.

Looking at the code below one might wonder why the vars needed for DataBinding are being passed here, and not in CreateChildControls().

protected override void PerformDataBinding(IEnumerable data) {
...
ListControl listControl;
//----------
listControl = this.ContainerMain.ListControl_Categories;
listControl.DataSourceID = this.DataSourceID_Categories;
listControl.DataSource = this.DataSource_Categories;
listControl.DataMember = this.DataMember_Categories;
listControl.DataTextField = this.DataBindingFieldNames.CategoryTextField;
listControl.DataValueField = this.DataBindingFieldNames.CategoryValueField;
...
if (listControl.DataSource != null) {
    listControl.DataBind();
}
}
powered by metaPost
Page: 3 of 3
Previous Page | Next Page

Comments

By Michel @ Tuesday, October 06, 2009 11:56 PM
Thanks! Good article about persisting properties. This helped me a lot solving some issues with my server control!

Click here to post a comment

             
Copyright 2007 by Sky Sigal