如何在回发时存储ListBox的所有属性值?(asp.net)

时间:2017-01-04 07:55:01

标签: c# asp.net listbox override custom-controls

我这里有问题! 嗨亲爱的专家。

我有一个asp:ListBox的自定义控件,代码如下:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExcelLikeFilterGridView
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:AADList runat=server></{0}:AADList>")]
    public class AADListBox : ListBox
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        protected override object SaveViewState()
        {
            // create object array for Item count + 1
            object[] allStates = new object[this.Items.Count + 1];

            // the +1 is to hold the base info
            object baseState = base.SaveViewState();
            allStates[0] = baseState;

            Int32 i = 1;
            // now loop through and save each Style attribute for the List
            foreach (ListItem li in this.Items)
            {
                Int32 j = 0;
                string[][] attributes = new string[li.Attributes.Count][];
                foreach (string attribute in li.Attributes.Keys)
                {
                    attributes[j++] = new string[] { attribute, li.Attributes[attribute] };
                }
                allStates[i++] = attributes;
            }
            return allStates;
        }
        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] myState = (object[])savedState;

                // restore base first
                if (myState[0] != null)
                    base.LoadViewState(myState[0]);

                Int32 i = 1;
                foreach (ListItem li in this.Items)
                {
                    // loop through and restore each style attribute
                    foreach (string[] attribute in (string[][])myState[i++])
                    {
                        li.Attributes[attribute[0]] = attribute[1];
                    }
                }
            }
        }
    }
}

在我的Override方法中,我希望在回发中获取每个ListBoxItem的所有属性。 这是我的代码背后:

foreach (DataColumn col in dv.Table.Columns)
{
                ListItemCollection Fields = new ListItemCollection();
                ListItem lst = new ListItem(col.ColumnName.ToString(), col.DataType.Name);
                if (col.DataType.Name == "String")
                {
                    lst.Attributes.Add("db-control-like", "");
                    lst.Attributes.Add("db-control-equal", "");
                }
                else if (col.DataType.Name == "Decimal" || col.DataType.Name == "Int32")
                {
                    lst.Attributes.Add("db-control-from", "");
                    lst.Attributes.Add("db-control-to", "");
                }
                Fields.Add(lst);
                lstboxFields.Items.AddRange(Fields.Cast<ListItem>().ToArray());
}

这是我的HTML输出:

    <select size="4" name="lstboxFields" id="lstboxFields" class="lstbox fields">
    <option value="Decimal" db-control-from="12" db-control-to="123123">fg_SysCode</option>
    <option value="Decimal" db-control-from="asda" db-control-to="12312">fg_GrMaster</option>
    <option value="String" db-control-equal="GridLink2,GridLink1" db-control-like="2321312">fg_GrName</option>
    <option value="Int32" db-control-from="1" db-control-to="2">fg_Len</option>
</select>

我使用javascript设置属性值,然后在回发时,第一个ListItem属性还在这里(注意:WithOut值)。

像这样:

<select size="4" name="lstboxFields" id="lstboxFields" class="lstbox fields">
<option selected="selected" value="Decimal" db-control-from="" db-control-to="">fg_SysCode</option>
<option value="Decimal" db-control-from="" db-control-to="">fg_GrMaster</option>
<option value="String" db-control-like="" db-control-equal="">fg_GrName</option>
<option value="Int32" db-control-from="" db-control-to="">fg_Len</option>
</select>

SO

我的覆盖问题在哪里? 我希望在回发中拥有所有属性值..! 请帮帮我 谢谢大家

0 个答案:

没有答案