在 asp.net 中使用数据绑定使用 optgroup 元素填充 HtmlSelect
我正在使用 asp.net,并且我的页面上有一个 HtmlSelect
元素(带有 runat="server"
)。通常我只会将 DataSource
设置为一些加载的数据和 DataBind
。然而,在本例中,数据具有一层层次结构,我想在 HTML 中使用 optgroup
来表示这一点。谷歌还没有带来任何喜悦——这可能吗?
I am using asp.net and have a HtmlSelect
element on my page (with runat="server"
). Normally I will just set the DataSource
to some loaded data and DataBind
. However in this case the data has one level of hierarchy and I want to represent this in the HTML with an optgroup
. Google hasn't come up with any joy - is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的问题需要解决,这就是我所做的。在继续之前,我确实找到了各种列表的重新分解方法,这些方法可能有效。但是,我认为这不是我的解决方案的最佳途径。这就是我所做的:
我还需要知道用户在下拉列表中选择了哪个选项,但发现无法在 ASP.net 中使用选项组构建下拉列表。因此,为了做到这一点,我只是构建了一个简单的
HTML 中的代码 (aspx):
在隐藏代码 (C#) 中:
I've had a similar problem to solve and this is how I did it. Before I continue I did find all sorts of re-fracturing methods for lists, which probably do work. However, I did not think this the best route to go for my solution. Here's what I did:
I also needed to know which option a user selected in the drop down list but found that you can't build drop down list with option groups in ASP.net. So to do this I just built a simple
<select>
with<optgroup>
's and<option>
's in each group. Then I placed a<div>
with a hidden textbox, which had therunat="server"
set. I set the"onchange"
event, on the select, to change the text value of the hidden textbox to whatever the value the option was, which was selected by the user, using javascript. Then, when the post back occurs, the code behind had access to the value the user selected, via the hidden textbox'srunat="server"
. Problem solved! The code looks something like this:Code in the HTML (aspx):
In the Code behind (C#):
我一直在寻找同样的东西,看起来 ASP 本身并不支持这个。一项 Google 搜索发现有人推荐 http://www.codeplex.com/SharpPieces 上的一个库,但我我从来没有用过它,我不知道它有多好。其他人谈论编写自己的渲染器以支持 optgroup。
I've been looking around for the same thing, and it doesn't look like ASP supports this natively. One Google search found someone recommending a library at http://www.codeplex.com/SharpPieces, but I've never used it and I don't know how good it is. Other people talk about writing your own renderer for optgroup support.