2012-10-11 201 views
3

我想將列表框中的所有項目添加到列表中(.cs頁面)。我使用javascript.Now將列表項目添加到列表框中。現在LstScan.Items.Count返回零。不要將項目添加到listbox.Listbox顯示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)); 
} 
+0

嘗試瀏覽器只回發的值選定的選項,而不是完整的選項列表。您需要將選項列表值存儲在回發中包含的隱藏字段中。請注意,您可能需要將['EnableEventValidation'](http://msdn.microsoft.com/zh-cn/library/system.web.ui.page.enableeventvalidation(v = vs80).aspx)設置爲'假這個頁面。 –

回答

0

你可以用文本

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

我已經嘗試過text.Still沒有結果 – user1737502

+0

@ tim對不起。你能告訴我如何將整個項目添加到hiddenfields – user1737502