asp.net - 无法将列表框中的项目添加到列表

标签 asp.net list listbox

我正在尝试将列表框中的所有项目添加到列表(.cs 页面)。我使用 javascript 将项目添加到列表框。现在 LstScan.Items.Count 始终返回零。我无法将项目添加到列表框。列表框显示values.but 问题发生在我的 .cs 文件中。 这是我的代码;

HTML 和 javascript 代码是:

 <script type="text/javascript">
      function displayKeyCode_demo(e) {
           var code;
           if (!e) var e = window.event;
           if (e.keyCode) code = e.keyCode;
           else if (e.which) code = e.which;
           if (code == 13) {
                var textbox = document.getElementById("<%=TextBox1.ClientID%>")
                var listbox = document.getElementById("<%=LstScan.ClientID%>")
                var newOption = document.createElement("Option");
                newOption.value = textbox.value;
                newOption.innerHTML = textbox.value;
                listbox.add(newOption);     
           }
      }
 </script>

 <div>
      <table cellspacing="3px" style="margin: 10px auto 3px 85px;">
           <tr>
                <td width="150px">
                     <asp:TextBox ID="TextBox1" runat="server" onkeypress="displayKeyCode_demo(event)"></asp:TextBox>
                </td>
                <td width="200px">
                     <asp:ListBox ID="LstScan" runat="server" Height="200px" Width="150px" AppendDataBoundItems="True" AutoPostBack="True" >
                     </asp:ListBox>
                </td>
           </tr>
      </table>
 </div>

这是我的代码背后的代码:

 List<int> LstSOnum = new List<int>();
 foreach (ListItem item in LstScan.Items) {
      LstSOnum.Add(int.Parse(item.Value));
 }

最佳答案

你可以试试文字

 LstSOnum.Add(int.Parse(item.Text));

关于asp.net - 无法将列表框中的项目添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836228/

相关文章:

asp.net - 已部署的 ASP.Net 站点中的可编辑资源文件

html - 如何切断比可用空间长的文本并显示 '...' ?

asp.net - 单击事件无法识别asp.net中的文本框

python - 在python中过滤一个填充为None的元组列表

silverlight - window 电话 7 : Highlight Selected Listbox item

python - 在列表框中显示组合框中的用户选择时出现问题

c# - 从项目目录中读取 SQL 查询文件

c++ - 当元素是指针或引用时,连续内存 vector 没有优势吗?

我的 for 循环中出现 java.lang.OutOfMemoryError

c# - 虚拟化堆栈面板返回空选定项