Browse DevX
Sign up for e-mail newsletters from DevX


Making Connections: Binding Controls to Custom Data Sources in .NET : Page 2

Anyone can bind a DataGrid to a DataSet in .NET, but the fact that you can bind almost any control property to almost any suitable object property—even to custom objects or custom object collections—is far less widely known.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Creating More Complex Bindings
You aren't limited to simple value/control property bindings. Here's an extension of the preceding example that changes the property type to a collection of stings that become the values of a DropDownList control. The .aspx page code looks like this:

<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# MyStrings %>"> </asp:DropDownList>

The supporting class, SimpleCollectionBinding, looks like this:

public class SimpleCollectionBinding : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; public StringCollection MyStrings { get { StringCollection col = new StringCollection(); col.Add("Option 1"); col.Add("Option 2"); col.Add("Option 3"); return col; } } private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { DataBind(); } } #region Web Form Designer generated code #endregion }

The SimpleCollectionBinding code makes a direct reference to the collection, so the property can have protected rather than public scope. During the call to DataBind, the DropDownList object looks for one of its supported interfaces (IEnumerable or IList) and once found, iterates over the objects in the collection calling the ToString() method to obtain the text values for the control to use. Again this is a very simple example which does not make use of the DataMember, DataTextField, DataTextFieldFormatString, or DataValueField properties of the control.

Now that you've seen how to bind a collection, here's a more complex object that can be used to fill the DropDownList. The DropDownListItem class shown below fills the values of the DropDownList control. When filled, the control should display a list of dates obtained from the class's Date property and should use the index value internally to identify which item the user has chosen. .

public class DropDownListItem { public DropDownListItem(DateTime date, int index) { this.date = date; this.index = index; } public DateTime Date { get { return this.date; } } public int Index { get { return this.index; } } private DateTime date; private int index; }

The form code contains a single property that the DropDownList control uses as its data source:

public class SimpleCollectionBinding2 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; public SimpleCollectionBinding2() { // Build a collection of collections. // The first collection contains a single // named member matching the DataMember property // value. In this case 'DataMember' // The second collection contains the values. this.dataSource = new DropDownListItemArray(); this.dataSource.Add(new DropDownListItem( DateTime.Now, 0)); this.dataSource.Add(new DropDownListItem( DateTime.Now.AddDays(1), 1)); this.dataSource.Add(new DropDownListItem( DateTime.Now.AddDays(2), 2)); } private DropDownListItemArray dataSource; public DropDownListItemArray DataSource { get { return this.dataSource; } } private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { DataBind(); } } #region Web Form Designer generated code #endregion }

To complete the picture, the DropDownList control references the Form's DataSource property using the Date property for the displayed text values and the Index property as the corresponding DataValue.

<asp:dropdownlist id=DropDownList1 runat="server" DataSource="<%# DataSource %>" DataTextField="Date" DataTextFormatString="Date {0:d}" DataValueField="Index"> </asp:dropdownlist>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date