ASP.NET Server control with an additional bindable field

旧巷老猫 提交于 2019-12-04 14:57:54

Here is a version I wrote a year or so ago. I wanted to be able to bind the checked status as well as a tooltip for the individual items. Hope it helps...

public class CheckBoxList_Extended : CheckBoxList
{
    /// <summary>
    /// Gets or sets the name of the data property to bind to the tooltip attribute of the individual CheckBox.
    /// </summary>
    [DefaultValue("")]
    public string DataTooltipField
    {
        get
        {
            string value = base.ViewState["DataTooltipField"] as string;
            if (value == null)
                value = "";
            return value;
        }
        set
        {
            if (value == null || value.Trim() == "")
            {
                base.ViewState.Remove("DataTooltipField");
            }
            else
            {
                base.ViewState["DataTooltipField"] = value.Trim();
            }
        }
    }
    /// <summary>
    /// Gets or sets the name of the data property to bind to the Checked property of the individual CheckBox.
    /// </summary>
    [DefaultValue("")]
    public string DataCheckedField
    {
        get
        {
            string value = base.ViewState["DataCheckedField"] as string;
            if (value == null)
                value = "";
            return value;
        }
        set
        {
            if (value == null || value.Trim() == "")
            {
                base.ViewState.Remove("DataCheckedField");
            }
            else
            {
                base.ViewState["DataCheckedField"] = value.Trim();
            }
        }
    }

    protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
    {
        if (dataSource != null)
        {
            string dataSelectedField = this.DataCheckedField;
            string dataTextField = this.DataTextField;
            string dataTooltipField = this.DataTooltipField;
            string dataValueField = this.DataValueField;
            string dataTextFormatString = this.DataTextFormatString;

            bool dataBindingFieldsSupplied = (dataTextField.Length != 0) || (dataValueField.Length != 0);
            bool hasTextFormatString = dataTextFormatString.Length != 0;
            bool hasTooltipField = dataTooltipField.Length != 0;
            bool hasSelectedField = dataSelectedField.Length != 0;

            if (!this.AppendDataBoundItems)
                this.Items.Clear();

            if (dataSource is ICollection)
                this.Items.Capacity = (dataSource as ICollection).Count + this.Items.Count;

            foreach (object dataItem in dataSource)
            {
                ListItem item = new ListItem();

                if (dataBindingFieldsSupplied)
                {
                    if (dataTextField.Length > 0)
                    {
                        item.Text = DataBinder.GetPropertyValue(dataItem, dataTextField, null);
                    }
                    if (dataValueField.Length > 0)
                    {
                        item.Value = DataBinder.GetPropertyValue(dataItem, dataValueField, null);
                    }
                }
                else
                {
                    if (hasTextFormatString)
                    {
                        item.Text = string.Format(CultureInfo.CurrentCulture, dataTextFormatString, new object[] { dataItem });
                    }
                    else
                    {
                        item.Text = dataItem.ToString();
                    }
                    item.Value = dataItem.ToString();
                }
                if (hasSelectedField)
                {
                    item.Selected = (bool)DataBinder.GetPropertyValue(dataItem, dataSelectedField);
                }
                if (hasTooltipField)
                {
                    string tooltip = DataBinder.GetPropertyValue(dataItem, dataTooltipField, null);
                    if (tooltip != null && tooltip.Trim() != "")
                    {
                        item.Attributes["title"] = tooltip;
                    }
                }
                this.Items.Add(item);
            }
        }
        base.PerformDataBinding(null);
    }
}

Checkbox already has a property for that, "Checked"

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checked.aspx

You can add a custom fairly easily though, just add a new public property. You can then set it programatically or in the aspx code.

public class ListedCheckBoxList : CheckBoxList
{
    public string CustomTag { get; set; }
    //...snip
}

<myControls:myCheckBox runat='server' Checked='True' CustomTag="123test" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!