Custom Data Binding for Server Controls
page 4 of 7
by Justin Lovell
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35700/ 73

Adding something more essential to our Knowledge

Now we are going to apply our knowledge into practise. I will develop a simple server control that will accept a title field and a description field from the data binding. The only way that this list control can be populated is through data binding. I have provided a simple collection editor and such in this server control because this is a FAQ at the ww.asp.net forums.

 

Now, let’s get the item server control on the roll. Basically, I will create a composite server control with a title and a description labels. Here is the basic code for the child and the parent controls (will not be explained because previous articles have gone over these aspects):

 

public class TitleDescriptionItem : Control, INamingContainer {
   private DataRow pDataItem;
   protected Label cTitle = new Label();
   protected Label cDescription = new Label();


   // will be used later with the data binding logic
   public DataRow DataItem {
      get { return pDataItem; }
      set { pDataItem = value; }
   }


   // provides a wrapper property for the "title" control
   public string Title {
      get { return cTitle.Text; }
      set { cTitle.Text = value; }
   }


   // provides a wrapper property for the "description" control
   public string Description {
      get { return cDescription.Text; }
      set { cDescription.Text = value; }
   }


   protected override void CreateChildControls() {
      cTitle.Font.Bold = true;
      cTitle.ForeColor = Color.Blue;

      Controls.Add(cTitle);
      Controls.Add(new LiteralControl("<br>"));
      Controls.Add(cDescription);
   }
     
   // TODO: Binding logic (listed later in article)
}


public class TitleDescription : Control, INamingContainer {
   // TODO: Binding logic (listed later in article)
}

 

Now to spice up the TitleDescription control and the TitleDescriptionItem control. I am just going to add a few attributes. The objective is to allow a collection editor to add items to the TitleDescription control. Firstly, let’s do the simple modification to TitleDescriptionItem control:

 

[DefaultProperty("Title")]
public class TitleDescriptionItem : Control, INamingContainer {
   // ...
}

 

In the above code, it will give will the collection editor a “name” for the item. Now, to add a few more stuff to the TitleDescription control:

 

[ParseChildren(true, "Items")]
[PersistChildren(false)]
public class TitleDescription : Control, INamingContainer {
   private ControlCollection pItems;


   [NotifyParentProperty(true)]
   [PersistenceMode(PersistenceMode.InnerProperty)]
   [DesignerSerializationVisibility
      (DesignerSerializationVisibility.Content)]
   public ControlCollection Items {
      get { return pItems; }
   }


   // ...


   public TitleDescription() {
      pItems = new ControlCollection(this);
   }
}

 

The ParseChildren attribute and the PersistChildren attribute are there to tell the ASP.NET parser that the stuff that will appear between the control’s tags are not part of the control – it says to parse all the information off to the Items property.

 

And finally, the attributes on the Items property says to the ASP.NET parser that the data should appear in inner tags – yet seen as a property. The above will allow the syntax on the ASP.NET page:

 

<jlc:TitleDescription id="TitleDescription1" runat="server">
   <jlc:TitleDscription Title="Me" Description="Hello, my name is Justin" />
   <jlc:TitleDscription Title="You" Description="Hi, I am ..." />
</jlc:TitleDescription>

 

You could read a blog post that I have already made and ask any further questions there. Now that I shed some light on a rather simple matter, yet can be confusing, let’s get back onto track about data binding.


View Entire Article

User Comments

Title: Great Article!   
Name: NiQuil
Date: 2010-01-18 9:53:29 AM
Comment:
This helped me a lot! I am totally new to programming and this has been a great tutorial for me.
I am creating my own control library with databound controls, with XHTML output and taking into account the 125 (very) strict rules for the Dutch Government Guidelines for the web.

I noticed this is a Visual studio 2003 (?) project since I had to convert it to my 2008.

Isthis still "the way to go" on this subject, or have new developments, maybe on Microsoft's part, on this subject seen the light of day?

Kind regards,

NiQuil
Title: Why Doesn't It Work   
Name: Marc
Date: 2009-06-04 3:09:50 PM
Comment:
My TitleDescriptionItems never display. CreateChildControlsnever gets hit. Why?
Title: DataSourceID   
Name: Andrew
Date: 2008-06-20 11:11:26 AM
Comment:
Great article, however I was wondering how would one implement the DataSourceID and DataMember properties?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-02-28 11:41:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search