DropDownList中的ListItems属性在回发时丢失?

问题描述 投票:69回答:11

一个同事给我看了这个:

他在网页上有一个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;
    }

在页面加载时,将显示项目的工具提示,但是在第一次回发时,属性会丢失。为什么会这样,并且有任何解决方法?

asp.net drop-down-menu postback behavior listitem
11个回答
70
投票

我有同样的问题,并希望贡献this资源,其中作者创建了一个继承的ListItem使用者以将属性持久保存到ViewState。希望它可以节省我浪费的时间,直到我偶然发现它。

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

0
投票

没有ViewState的简单解决方案,创建新的服务器控件或复杂的smth:

正在创建:

public void AddItemList(DropDownList list, string text, string value, string group = null, string type = null)
{
    var item = new ListItem(text, value);

    if (!string.IsNullOrEmpty(group))
    {
        if (string.IsNullOrEmpty(type)) type = "group";
        item.Attributes["data-" + type] = group;
    }

    list.Items.Add(item);
}

更新中:

public void ChangeItemList(DropDownList list, string eq, string group = null, string type = null)
{
    var listItem = list.Items.Cast<ListItem>().First(item => item.Value == eq);

    if (!string.IsNullOrEmpty(group))
    {
        if (string.IsNullOrEmpty(type)) type = "group";
        listItem.Attributes["data-" + type] = group;    
    }
}

示例:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        using (var context = new WOContext())
        {
            context.Report_Types.ToList().ForEach(types => AddItemList(DropDownList1, types.Name, types.ID.ToString(), types.ReportBaseTypes.Name));
            DropDownList1.DataBind();
        }
    }
    else
    {
        using (var context = new WOContext())
        {
            context.Report_Types.ToList().ForEach(types => ChangeItemList(DropDownList1, types.ID.ToString(), types.ReportBaseTypes.Name));
        }
    }
}

0
投票

我设法通过使用会话变量来实现这一点,在我的情况下,我的列表将不会包含很多元素,因此它工作得很好,这就是我做到的方式:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] elems;//Array with values to add to the list
        for (int q = 0; q < elems.Length; q++)
        {
            ListItem li = new ListItem() { Value = "text", Text = "text" };
            li.Attributes["data-image"] = elems[q];
            myList.Items.Add(li);
            HttpContext.Current.Session.Add("attr" + q, elems[q]);
        }
    }
    else
    {
        for (int o = 0; o < webmenu.Items.Count; o++) 
        {
            myList.Items[o].Attributes["data-image"] = HttpContext.Current.Session["attr" + o].ToString();
        }
    }
}

[第一次加载页面时,将填充列表,并添加一个Image属性,该属性在回发后会丢失:(因此,在添加具有其属性的元素时,我创建了一个Session变量“ attr”加上编号元素取自“ for”循环(类似于attr0,attr1,attr2等),在其中保存了回发发生时(内部)的属性值(本例中为图像的路径)我只是循环列表,并使用“ for”循环的“ int”添加从Session变量中获取的属性,该属性与加载页面时相同(这是因为在此页面中,我没有只需选择即可将元素添加到列表中,以便它们始终具有相同的索引),然后再次设置属性,希望这对以后的工作有所帮助,问候!


36
投票

谢谢,拉勒米。正是我想要的。它使属性保持完美。

展开,下面是我使用Laramie的代码创建的类文件,以在VS2008中创建下拉列表。在App_Code文件夹中创建该类。创建类后,请在aspx页面上使用以下行进行注册:

<%@ Register TagPrefix="aspNewControls" Namespace="NewControls"%>

然后您可以使用此将控件放在您的Web窗体上

<aspNewControls:NewDropDownList ID="ddlWhatever" runat="server">
                                                </aspNewControls:NewDropDownList>

好,这是课程...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NewControls
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
  public class NewDropDownList : DropDownList
  {
    [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];
                }
            }
        }
    }
  }
}

13
投票

简单的解决方案是在下拉菜单的pre-render事件中添加工具提示属性。对状态的任何更改都应在pre-render事件中进行。

示例代码:

protected void drpBrand_PreRender(object sender, EventArgs e)
        {
            foreach (ListItem _listItem in drpBrand.Items)
            {
                _listItem.Attributes.Add("title", _listItem.Text);
            }
            drpBrand.Attributes.Add("onmouseover", "this.title=this.options[this.selectedIndex].title");
        }

8
投票

如果只想在页面的第一次加载时加载列表项,则需要启用ViewState,以便控件可以在那里序列化其状态并在页面回发时重新加载它。

[可以在多个位置启用ViewState-检查web.config中的<pages/>节点以及aspx文件本身顶部的<%@ page %>指令中的EnableViewState属性。要使ViewState起作用,此设置必须为true

如果您不想使用ViewState,只需从添加if (!IsPostBack) { ... }的代码中删除ListItems,然后将在每次回发中重新创建项目。

编辑:很抱歉-我误读了你的问题。您是正确的,因为attributes不会在回发中幸免,因为它们未在ViewState中序列化。您必须在每个回发中重新添加这些属性。


6
投票

一种简单的解决方案-在您要求回发的click事件上调用下拉加载功能。


2
投票

此问题的典型解决方案涉及创建在正常情况下不太可行的新控件。有一个简单但不重要的解决方案。

问题是ListItem在回发时会丢失其属性。但是,列表本身永远不会丢失任何自定义属性。因此,可以以一种简单而有效的方式利用这一点。

步骤:

  1. 使用以上答案中的代码对您的属性进行序列化(https://stackoverflow.com/a/3099755/3624833

  2. 将其存储到ListControl的自定义属性(下拉列表,清单框等)。

  3. 在回发时,从ListControl读取自定义属性,然后将其反序列化为属性。

这是我用来对属性进行反序列化的代码(我需要做的是跟踪从后端检索时最初呈现为选定状态的列表的哪些项目,然后根据更改保存或删除行由用户在UI上完成):

string[] selections = new string[Users.Items.Count];
for(int i = 0; i < Users.Items.Count; i++)
{
    selections[i] = string.Format("{0};{1}", Users.Items[i].Value, Users.Items[i].Selected);
}
Users.Attributes["data-item-previous-states"] = string.Join("|", selections);

((“用户”是CheckboxList控件)。

回发时(在我的情况下为Submit按钮Click事件),我使用下面的代码来检索它们并将它们存储到Dictionary中以进行后处理:

Dictionary<Guid, bool> previousStates = new Dictionary<Guid, bool>();
string[] state = Users.Attributes["data-item-previous-states"].Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
foreach(string obj in state)
{
    string[] kv = obj.Split(new char[] { ';' }, StringSplitOptions.None);
    previousStates.Add(kv[0], kv[1]);
}

((PS:我有一个执行错误处理和数据转换的库函式,为简洁起见,在此省略了它)。


1
投票

@ Sujay您可以将分号分隔的文本添加到下拉列表的value属性中(例如csv样式),然后使用String.Split(';')从一个值中获取2个“值”,作为一种解决方法,可以避免必须创建一个新的用户控件。尤其是在您只有很少的额外属性且时间不长的情况下。您还可以在下拉列表的value属性中使用JSON值,然后从那里解析出所需的内容。


1
投票

这是Laramie提出并由gleapman完善的解决方案的VB.Net代码。

更新:我在下面发布的代码实际上是用于ListBox控件的。只需将继承更改为DropDownList并重命名该类即可。

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Linq
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomControls

<DefaultProperty("Text")> _
<ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")>
Public Class PersistentListBox
    Inherits ListBox

    <Bindable(True)> _
    <Category("Appearance")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    Protected Overrides Function SaveViewState() As Object
        ' Create object array for Item count + 1
        Dim allStates As Object() = New Object(Me.Items.Count + 1) {}

        ' The +1 is to hold the base info
        Dim baseState As Object = MyBase.SaveViewState()
        allStates(0) = baseState

        Dim i As Int32 = 1
        ' Now loop through and save each attribute for the List
        For Each li As ListItem In Me.Items
            Dim j As Int32 = 0
            Dim attributes As String()() = New String(li.Attributes.Count - 1)() {}
            For Each attribute As String In li.Attributes.Keys
                attributes(j) = New String() {attribute, li.Attributes(attribute)}
                j += 1
            Next
            allStates(i) = attributes
            i += 1
        Next


        Return allStates
    End Function

    Protected Overrides Sub LoadViewState(savedState As Object)
        If savedState IsNot Nothing Then
            Dim myState As Object() = DirectCast(savedState, Object())

            ' Restore base first
            If myState(0) IsNot Nothing Then
                MyBase.LoadViewState(myState(0))
            End If

            Dim i As Int32 = 0
            For Each li As ListItem In Me.Items
                ' Loop through and restore each attribute 
                ' NOTE: Ignore the first item as that is the base state and is represented by a Triplet struct
                i += 1
                For Each attribute As String() In DirectCast(myState(i), String()())
                    li.Attributes(attribute(0)) = attribute(1)
                Next
            Next
        End If
    End Sub
End Class
End Namespace

0
投票
    //In the same block where the ddl is loaded (assuming the dataview is retrieved whether postback or not), search for the listitem and re-apply the attribute
    if(IsPostBack)
    foreach (DataRow dr in dvFacility.Table.Rows)
{                        
   //search the listitem 
   ListItem li = ddl_FacilityFilter.Items.FindByValue(dr["FACILITY_CD"].ToString());
    if (li!=null)
 {
  li.Attributes.Add("Title", dr["Facility_Description"].ToString());    
 }                  
} //end for each  
© www.soinside.com 2019 - 2024. All rights reserved.