在 asp.net 中使用数据绑定使用 optgroup 元素填充 HtmlSelect

发布于 2024-10-11 17:15:28 字数 235 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

感情旳空白 2024-10-18 17:15:28

我有一个类似的问题需要解决,这就是我所做的。在继续之前,我确实找到了各种列表的重新分解方法,这些方法可能有效。但是,我认为这不是我的解决方案的最佳途径。这就是我所做的:

我还需要知道用户在下拉列表中选择了哪个选项,但发现无法在 ASP.net 中使用选项组构建下拉列表。因此,为了做到这一点,我只是构建了一个简单的

HTML 中的代码 (aspx):

<div id="selectDiv">
    <select id="selectItems" onchange="document.getElementById('hiddenselectDiv').getElementsByTagName('input')[0].value = this.options[this.selectedIndex].value;">
        <optgroup label="Johannesburg">
            <option value="Wilropark">Wilropark</option>
            <option value="Bryanpark">Bryanpark</option>
            <option value="Hurlingham">Hurlingham</option>
            <option value="Midrand ">Midrand </option>
            <option value="Glenvista">Glenvista</option>
            <option value="Sunninghill">Sunninghill</option>
            <option value="Edenvale ">Edenvale </option>
            <option value="Parkhurst">Parkhurst</option>
        </optgroup>
        <optgroup label="Cape Town">
            <option value="Tokai">Tokai</option>
            <option value="Durbanville">Durbanville</option>
        </optgroup>
        <optgroup label="Durban">
            <option value="Musgrave">Musgrave</option>
        </optgroup>
        <optgroup label="Pretoria">
            <option value="Hatfield">Hatfield</option>
        </optgroup>
    </select>
</div>
<div id="hiddenSelectDiv">
    <!--
    Note: You probably want to set the hidden value to the first item you have seleced when you build the <select> object.
    -->
    <input type="hidden" id="selectedItem" value="Wilropark">
</div>

在隐藏代码 (C#) 中:

if (!string.IsNullOrEmpty(selectedItem.Value))
{
    //validation or whatever you want....
}

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 the runat="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's runat="server". Problem solved! The code looks something like this:

Code in the HTML (aspx):

<div id="selectDiv">
    <select id="selectItems" onchange="document.getElementById('hiddenselectDiv').getElementsByTagName('input')[0].value = this.options[this.selectedIndex].value;">
        <optgroup label="Johannesburg">
            <option value="Wilropark">Wilropark</option>
            <option value="Bryanpark">Bryanpark</option>
            <option value="Hurlingham">Hurlingham</option>
            <option value="Midrand ">Midrand </option>
            <option value="Glenvista">Glenvista</option>
            <option value="Sunninghill">Sunninghill</option>
            <option value="Edenvale ">Edenvale </option>
            <option value="Parkhurst">Parkhurst</option>
        </optgroup>
        <optgroup label="Cape Town">
            <option value="Tokai">Tokai</option>
            <option value="Durbanville">Durbanville</option>
        </optgroup>
        <optgroup label="Durban">
            <option value="Musgrave">Musgrave</option>
        </optgroup>
        <optgroup label="Pretoria">
            <option value="Hatfield">Hatfield</option>
        </optgroup>
    </select>
</div>
<div id="hiddenSelectDiv">
    <!--
    Note: You probably want to set the hidden value to the first item you have seleced when you build the <select> object.
    -->
    <input type="hidden" id="selectedItem" value="Wilropark">
</div>

In the Code behind (C#):

if (!string.IsNullOrEmpty(selectedItem.Value))
{
    //validation or whatever you want....
}
蝶…霜飞 2024-10-18 17:15:28

我一直在寻找同样的东西,看起来 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.

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