c# - 具有附加可绑定(bind)字段的 ASP.NET 服务器控件

标签 c# asp.net asp.net-controls

我创建了一个自定义服务器控件,派生自 System.Web.Contols.CheckBoxList 以自定义如何呈现 CheckBoxList。我还想添加另一个可绑定(bind)字段并在 CheckBoxList.RenderItem() 方法中获取该字段的值。我要创建的字段应包含一个值,该值指定是否检查了 CheckBoxListItem。我读过一些关于自定义数据字段的文章,但从未对其进行详细解释。

为了更好地解释我似乎无法理解的内容,我加入了一部分类(class)。

public class ListedCheckBoxList : CheckBoxList
{
    protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
        if (itemType != ListItemType.Item)
            return;

        var item = base.Items[repeatIndex];

        string cbxHtml = string.Format("<input type=\"checkbox\" value=\"{0}\" name=\"{1}\" /> {2}",
            item.Value,
            string.Concat(this.ClientID, repeatIndex),
            item.IsChecked, // <-- My custom bindable field
            item.Text);

        writer.Write(cbxHtml);
    }
}

在 .aspx 页面中使用此控件时,我试图像这样绑定(bind)它

<abc:ListedCheckBoxList ID="cbxList" runat="server"
     DataValueField="UserId"
     DataTextField="UserFullName"
     DataIsCheckedField="UserIsActive" />

最佳答案

这是我大约一年前写的版本。我希望能够绑定(bind)检查状态以及单个项目的工具提示。希望对您有所帮助...

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);
    }
}

关于c# - 具有附加可绑定(bind)字段的 ASP.NET 服务器控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5409664/

相关文章:

c# - 如何隐藏派生类的扩展方法?

c# - 使用 C# 在 asp.net 中以 Word 格式转换货币格式的 int 值?

c# - .net 中的 TLS/SSL

c# - 在 C# 中使用来自单独 .cs 文件的变量和方法

javascript - 使用 javascript 拖放

c# - 找不到类型或命名空间名称 'ApplicationUser'

c# - 如何迭代 ASP.NET 页面内的控件?

asp.net - 在已创建的 DataTable 对象上使用 SELECT DISTINCT?

javascript - 如何将脚本正确添加到控件?

c# - 为什么无限循环不会使 CPU 使用率达到 100