ASP.NET dropdownlist 控件可以在回发时保留列表项类吗?

发布于 2024-10-26 11:36:46 字数 1317 浏览 1 评论 0原文

我在页面上有一个下拉列表控件,并将 AutoPostBack 设置为 True。在页面初始加载时,我在 DropDownList 中的某些 ListItems 上设置 CSS 类。生成的 HTML 如下所示:

<select id="mySelect">
    <option value="1">First</option>
    <option value="2" selected="selected">Second</option>
    <option value="3" class="favorite">Third</option>
    <option value="4">Fourth</option>
    <option value="5" class="favorite">Fifth</option>
</select>

回发后,ListItems 会丢失其 CSS 类。现在看起来像这样。

<select id="mySelect">
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
    <option value="4">Fourth</option>
    <option value="5" selected="selected">Fifth</option>
</select>

有什么方法可以让下拉列表在回发后记住各个 ListItems 上的 CSS 类,或者我需要在回发后以某种方式自己设置这些类吗?

以下是将 CSS 添加到下拉列表的代码。它在 PageLoad 上运行,但不在 PostBack 上运行。

foreach(MyItem _myItem in MyItemList)
{
   ListItem _listItem = new ListItem();
   _listItem.Value = _myItem.ID.ToString();
   _listItem.Text = _myItem.Title;
   if(_myItem.IsFavorite)
   {
      _list.Attributes["class"] = "favorite";
   }
   ddlMyDropDown.Items.Add(_listItem);
}

科里

I have a dropdownlist control on a page with AutoPostBack set to True. On the initial load of the page I am setting a CSS class on certain ListItems in the DropDownList. The resulting HTML looks like this:

<select id="mySelect">
    <option value="1">First</option>
    <option value="2" selected="selected">Second</option>
    <option value="3" class="favorite">Third</option>
    <option value="4">Fourth</option>
    <option value="5" class="favorite">Fifth</option>
</select>

After postback the ListItems lose their CSS classes. It now looks like this.

<select id="mySelect">
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
    <option value="4">Fourth</option>
    <option value="5" selected="selected">Fifth</option>
</select>

Is there any way that the dropdownlist will remember the CSS classes on individual ListItems after a postback or will I need to somehow set the classes myself after postback?

Here is the code that adds the CSS to the dropdownlist. It is run on PageLoad but not run on PostBack.

foreach(MyItem _myItem in MyItemList)
{
   ListItem _listItem = new ListItem();
   _listItem.Value = _myItem.ID.ToString();
   _listItem.Text = _myItem.Title;
   if(_myItem.IsFavorite)
   {
      _list.Attributes["class"] = "favorite";
   }
   ddlMyDropDown.Items.Add(_listItem);
}

Corey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-11-02 11:36:46

编辑:

这对我有用。尝试这样的事情

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadData();
        } 

        LoadStyle();
    }

    private void LoadData()
    {
        DropDownList1.Items.AddRange(Enumerable.Range(0, 10).Select(x => new ListItem(x.ToString())).ToArray());
    }

    private void LoadStyle()
    {
        foreach (ListItem item in DropDownList1.Items)
        {
            if (int.Parse(item.Value) % 2 == 0)
            {
                item.Attributes.Add("class", "test");
            }
        }
    }

EDIT:

This works for me. Try something like this

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadData();
        } 

        LoadStyle();
    }

    private void LoadData()
    {
        DropDownList1.Items.AddRange(Enumerable.Range(0, 10).Select(x => new ListItem(x.ToString())).ToArray());
    }

    private void LoadStyle()
    {
        foreach (ListItem item in DropDownList1.Items)
        {
            if (int.Parse(item.Value) % 2 == 0)
            {
                item.Attributes.Add("class", "test");
            }
        }
    }
千笙结 2024-11-02 11:36:46

因此,您的问题是状态管理之一,并且在大多数情况下,您有责任管理页面上的项目的状态。考虑一下这个页面可能在一分钟内呈现 100 次(针对 100 个不同的浏览器),并且每个页面可能有不同的状态。服务器无法跟踪所有这些不同的实例和状态,您必须这样做。

有两种方法可以处理这个问题,最简单的方法类似于 @StackOverflowException 所说的,只需确保每次创建页面时都确定并设置状态即可。这更容易,但绝对不可扩展,特别是如果涉及大量数据库工作和其他内容。

更好的解决方案是创建一些您自己的属性,将内容存储在页面 ViewState 中。有很多方法可以做到这一点,但这里只是一个快速示例:

protected override OnPageLoad(EventArgs e)
{
    myListBox.Items.AddRange(myListItems);
}

protected IEnumberable<ListItems> myListItems 
{
    get 
    { 
        IEnumberable<ListItems> items = (IEnumerable<ListItems>)ViewState["myListItems"]; 

        // Build the list if we can't get it out of the viewstate
        if(items = null) 
        {
            items = BuildListItems();
            ViewState["myListItems"] = items;
        }

        return _items;          
    }

    set { ViewState["myListItems"] = value; }
}

这在初始页面视图上需要更长的时间,但在回发期间(理论上)会更快,因为它可以从页面的视图状态中获取列表无需重建它(基本上是反序列化它)。

此方法的缺点是,如果您在其中放入太多数据,则输出到浏览器的视图状态可能会变得非常大。

So your problem here is one of state management, and for the most part its your responsibility to manage the state of the items on your page. Consider that this one page may be rendered 100 times in a minute (to 100 different browsers) and each one could have a different state. The server can't keep track of all these different instances and states, you have to do that.

There are two ways to deal with this, the simplest is similar to what @StackOverflowException stated, just make sure that you determine and set your state every time your page is created. This is easier, but definitely not scalable, especially if that involves a lot of database work and other stuff.

The better solution is to create some properties of your own that store things in the pages ViewState. There are a lot of ways to do this, but here's just a quick sample:

protected override OnPageLoad(EventArgs e)
{
    myListBox.Items.AddRange(myListItems);
}

protected IEnumberable<ListItems> myListItems 
{
    get 
    { 
        IEnumberable<ListItems> items = (IEnumerable<ListItems>)ViewState["myListItems"]; 

        // Build the list if we can't get it out of the viewstate
        if(items = null) 
        {
            items = BuildListItems();
            ViewState["myListItems"] = items;
        }

        return _items;          
    }

    set { ViewState["myListItems"] = value; }
}

This would take a bit longer on the initial page view, but during postbacks would (theoretically) be faster because it can get the list out of the page's view state without reconstructing it (basically deserializing it).

The drawback to this method is that your view state, which is output to the browser, can get very large if you put too much data in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文