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): 28677/ 80

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

User Comments

Title: Article   
Name: bhaskar
Date: 2008-08-30 6:23:21 AM
Comment:
good






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


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-08 2:27:53 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search