“选择所选项目”返回 False

本文关键字:返回 False 项目 选项 选择 | 更新日期: 2023-09-27 17:56:30

我正在尝试从服务器端的选定选择中获取选定的项目。

这是我的 HTML:

<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<link href="Content/chosen.css" rel="stylesheet" />

<form id="form1" runat="server">
    <select id="chsn" runat="server" class="chzn-select" multiple="true" name="faculty" style="width: 200px;">
    </select>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <script src="Scripts/chosen.jquery.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".chzn-select").chosen();
            $(".chosen-select").chosen();
        });
    </script>
</form>

这是服务器端:

protected void Button1_Click(object sender, EventArgs e)
    {
        List<ListItem> tmpLst = new List<ListItem>();
        for (int i = 0; i < chsn.Items.Count; i++)
        {
            if (chsn.Items[i].Selected)
                tmpLst.Add(chsn.Items[i]);
        }
    }

中新网.项目[i].选择始终返回 false。有没有更好的方法来获取选定的项目?

“选择所选项目”返回 False

问题不在于您的Button1_Click事件。(我已经测试过了)。

问题一定出在将选择chsn与值绑定的 Page_Load 事件上。确保在!IsPostBack下绑定 HTMLSelect

>Naveen是对的..我只是回答这个问题,因为我虽然你可能不明白纳文斯的意思。

您的页面加载事件中的所有内容。把它放在这个检查下

if(!isPostBack)
{
//here comes the code you have in pageload event.
}