1

I have a problem here! Hi Dear Experts.

I have a custom control of asp:ListBox as Code Below :

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

In my Override methods i want to get all attributes from each ListBoxItem, on postbacks. here is my code behind :

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

here is my HTML OUTPUT :

    <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>

i set the attribute values with javascript and then on postback just first ListItem attribute's are here yet(Attention : WithOut Values).

Like this :

<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

Where is the problem in my overrides? I Want to Have All Attributes Values Back in postbacks..! Please help me on this Thanks all

hcerim
  • 959
  • 1
  • 11
  • 27
Amin AmiriDarban
  • 2,031
  • 4
  • 24
  • 32
  • By the Way, take a look at this, http://stackoverflow.com/questions/1313447/listitems-attributes-in-a-dropdownlist-are-lost-on-postback – Amin AmiriDarban Jan 04 '17 at 07:57

0 Answers0