ASP.NET dropdownlist 控件可以在回发时保留列表项类吗?
我在页面上有一个下拉列表控件,并将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
这对我有用。尝试这样的事情
EDIT:
This works for me. Try something like this
因此,您的问题是状态管理之一,并且在大多数情况下,您有责任管理页面上的项目的状态。考虑一下这个页面可能在一分钟内呈现 100 次(针对 100 个不同的浏览器),并且每个页面可能有不同的状态。服务器无法跟踪所有这些不同的实例和状态,您必须这样做。
有两种方法可以处理这个问题,最简单的方法类似于 @StackOverflowException 所说的,只需确保每次创建页面时都确定并设置状态即可。这更容易,但绝对不可扩展,特别是如果涉及大量数据库工作和其他内容。
更好的解决方案是创建一些您自己的属性,将内容存储在页面 ViewState 中。有很多方法可以做到这一点,但这里只是一个快速示例:
这在初始页面视图上需要更长的时间,但在回发期间(理论上)会更快,因为它可以从页面的视图状态中获取列表无需重建它(基本上是反序列化它)。
此方法的缺点是,如果您在其中放入太多数据,则输出到浏览器的视图状态可能会变得非常大。
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:
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.