简体   繁体   English

带有附加可绑定字段的ASP.NET Server控件

[英]ASP.NET Server control with an additional bindable field

I have created a custom server control, deriving from System.Web.Contols.CheckBoxList to customize how a CheckBoxList is rendered. 我创建了一个自定义服务器控件,派生自System.Web.Contols.CheckBoxList来自定义CheckBoxList的呈现方式。 I also wanted to add another bindable field and get the value of the field within the CheckBoxList.RenderItem() method. 我还想添加另一个可绑定字段并获取CheckBoxList.RenderItem()方法中的字段值。 The field I want to create, should contain a value specifying whether a CheckBoxListItem is checked. 我想要创建的字段应包含一个值,该值指定是否选中CheckBoxListItem I've read some articles regarding custom DataFields, but it never gets explained in detail. 我已经阅读了一些关于自定义DataFields的文章,但它从未详细解释过。

I've included a portion of my class to better explain what I can't seem to understand. 我已经把我课程的一部分包括在内,以便更好地解释我似乎无法理解的内容。

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

When using this control in the .aspx page, I'm attempting to bind it like this 在.aspx页面中使用此控件时,我试图像这样绑定它

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

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" Checkbox已经有了一个属性,“Checked”

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checked.aspx 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. 然后,您可以以编程方式或在aspx代码中设置它。

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

<myControls:myCheckBox runat='server' Checked='True' CustomTag="123test" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM