Creating Custom Data Field Controls - Part 1
page 3 of 5
by Brian Mains
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26136/ 47

Data Binding

When the control interface is created, an event handler is setup to respond to the data binding.  At this point, the control has not been bound and needs to be assigned a value. As you can see below, this method handles getting.

Listing 6

void control_DataBinding(object sender, EventArgs e) 
    if (sender is TableCell) 
        TableCell cell = sender as TableCell;
        cell.Text = this.GetDataItemValue<string>(cell.NamingContainer, 
        CheckBox box = control as CheckBox;
        if (!insertMode) 
            box.Checked = this.GetDataItemValue<bool>(box.NamingContainer,   

The GetDataItemValue is a method I illustrated above; it uses the naming container for the actual control, and performs the action to get the reference to the actual value, passing it into the Checked property. This assignment here is to the Checked property, but could also be an assignment to the Text property of a TextBox, the SelectedDate property of a Calendar, the SelectedValue property of a DropDownList, and many more. However, in readonly mode, the cell renders a readonly value, which uses the following method to render a more appropriate method.

Listing 7

protected virtual string GetReadOnlyValue(object initialValue) 
    if (initialValue != null)
        return initialValue.ToString(); 
        return null; 

This does not seem very helpful, but it can be very helpful in allowing a custom data field to render a formatted read-only value that is appropriate. For instance, for a boolean field I created, I used it to change the text from True/False database values to the values that are stored in two property values within the control (so by setting the TrueString and FalseString properties of the custom data field, it translates the database boolean value into a more readable form in read-only mode).  In addition, for a numerical field I created, I can use this to prefix the text with $ and suffix it with .00, making it more meaningful to show whole dollar amounts.

View Entire Article

User Comments

Title: A test title   
Name: Hassan
Date: 2008-06-30 10:37:25 AM
very good article.
Title: Awesome!   
Name: AzamSharp
Date: 2008-06-26 12:17:36 AM

Very nice article! I think I am going to look into creating some custom data field controls myself.
Title: Nice article   
Name: Joydip Kanjilal
Date: 2008-01-04 12:16:56 PM
Nice article Brian.



Author: ASP.NET Data Presentation Controls Essentials (Packt Publishing)

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

©Copyright 1998-2024  |  Page Processed at 2024-04-15 2:16:14 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search