Creating a Custom Data Field Control - Part 2
page 5 of 6
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 4007/ 151

The BooleanField Example

This example contains a similar approach to the DateTimeField mentioned above. It is actually simpler to implement because it does not have many challenges. For instance, here is the definition of the previous four methods as shown above.

Listing 12

public override void BindEditControl(object control, bool insertMode)
{
  CheckBox box = control as CheckBox;
  if (!insertMode)
    box.Checked = this.GetDataItemValue < bool > (box.NamingContainer,
      this.DataField);
}
 
protected override string GetDataItemFieldName()
{
  return this.DataField;
}
 
public override string GetEditControlValue(TableCell cell)
{
  return this.ExtractControl < CheckBox > (cell).Checked.ToString();
}
 
public override Control SetupEditControl()
{
  return new CheckBox();
}

Because the control is a checkbox, which the interface is not manipulated by internal properties, it is easy to handle the interactions. However, the CheckBoxField implements some of the additional functionality available. For instance, this custom class defines a TrueString and FalseString property values which convert the read-only value to something more appropriate.

To do this, it requires catching the value being bound to the table cell, and outputting a different value. I created a GetReadOnlyValue method that changes the initial value into something else, all at your control. The implementation that works with this custom field is shown below (note that the default to TrueString and FalseString are the bool.TrueString and bool.FalseString properties).

Listing 13

protected override string GetReadOnlyValue(object initialValue)
{
  if (initialValue == null)
    return bool.FalseString;
 
  bool value = (bool)initialValue;
  return value ? this.TrueString : this.FalseString;
}

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 1 and type the answer here:

User Comments

Title: good artical   
Name: sanjay
Date: 4/24/2008 12:47:33 AM
Comment:
sssss

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/5/2008 7:33:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search