DropDownList中的ListItems属性在回发时丢失? [英] ListItems attributes in a DropDownList are lost on postback?

查看:165
本文介绍了DropDownList中的ListItems属性在回发时丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个同事告诉我:



他在网页上有一个DropDownList和一个按钮。以下是代码:

  protected void Page_Load(object sender,EventArgs e)
{
if( !IsPostBack)
{
ListItem item = new ListItem(1);
item.Attributes.Add(title,A);

ListItem item2 = new ListItem(2);
item2.Attributes.Add(title,B);

DropDownList1.Items.AddRange(new [] {item,item2});
string s = DropDownList1.Items [0] .Attributes [title];
}
}

protected void Button1_Click(object sender,EventArgs e)
{
DropDownList1.Visible =!DropDownList1.Visible;
}

在页面加载中,项目的工具提示显示,但在第一个回发,属性丢失。为什么是这种情况,有没有解决方法?

解决方案

我有同样的问题,想贡献这个资源,作者创建一个继承的ListItem Consumer将属性持久化到ViewState。希望它会节省某人我被浪费的时间,直到我偶然发现。

 保护覆盖对象SaveViewState()
{
//创建Item count的对象数组+ 1
object [] allStates = new object [this.Items.Count + 1];

// +1保存基本信息
object baseState = base.SaveViewState();
allStates [0] = baseState;

Int32 i = 1;
//现在循环并保存List
foreach的每个Style属性(在this.Items中为ListItem li)
{
Int32 j = 0;
string [] [] attributes = new string [li.Attributes.Count] [];
foreach(li.Attributes.Keys中的字符串属性)
{
attributes [j ++] = new string [] {attribute,li.Attributes [attribute]};
}
allStates [i ++] =属性;
}
return allStates;
}

protected override void LoadViewState(object savedState)
{
if(savedState!= null)
{
object [] myState =(object [])savedState;

//恢复基本
如果(myState [0]!= null)
base.LoadViewState(myState [0]);

Int32 i = 1;
foreach(ListItem li in this.Items)
{
//循环并恢复每个样式属性
foreach(string [] attribute in(string [] [])myState [i ++])
{
li.Attributes [attribute [0]] = attribute [1];
}
}
}
}


A coworker showed me this:

He has a DropDownList and a button on a web page. Here's the code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListItem item = new ListItem("1");
            item.Attributes.Add("title", "A");

            ListItem item2 = new ListItem("2");
            item2.Attributes.Add("title", "B");

            DropDownList1.Items.AddRange(new[] {item, item2});
            string s = DropDownList1.Items[0].Attributes["title"];
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DropDownList1.Visible = !DropDownList1.Visible;
    }

On the page load, the items' tooltips are showing, but on the first postback, the attributes are lost. Why is this the case, and are there any workarounds?

解决方案

I had the same problem and wanted to contribute this resource where the author created an inherited ListItem Consumer to persist attributes to ViewState. Hopefully it will save someone the time I wasted until I stumbled on it.

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

这篇关于DropDownList中的ListItems属性在回发时丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆