[英]I can't get values of Listbox in asp.net after it was daynamically added in Javascript
I daynamically create options of select in javascript, and click a asp:Button to submit the added options.我每天在 javascript 中创建 select 选项,然后单击 asp:Button 以提交添加的选项。 but i can't get that from ListBox.但我无法从 ListBox 中得到它。 pls help me!!请帮助我!!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Hello.aspx.cs" Inherits="BeyondInfo.Hello" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to My First ASP.NET</title>
<script type="text/javascript">
function add() {
var count = document.getElementById("count");
if (count.value == "") {
count.value = 1;
}
count.value = parseInt(count.value) + 1;
var num = count.value;
var listBox2 = document.getElementById("ListBox2");
var optn = document.createElement("OPTION");
optn.value = num;
optn.text = num + "_add";
listBox2.options.add(optn);
}
</script>
</head>
<link href="Common.css" rel="stylesheet" />
<body>
<form id="form1" runat="server">
<div style="height: 277px">
<asp:ListBox ID="ListBox2" runat="server">
<asp:ListItem>b1222222222222222222222222222</asp:ListItem>
<asp:ListItem>b2</asp:ListItem>
<asp:ListItem>b3</asp:ListItem>
<asp:ListItem>b4</asp:ListItem>
</asp:ListBox>
<input id="count" type="hidden" />
<input id="btnAdd" type="button" value="ADD" onclick="add()"/>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
</body>
</html>
</p>
Hello.aspx.cs你好.aspx.cs
public partial class Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string sel = "";
foreach (ListItem item in ListBox2.Items) {
sel +=item.Text + ",";
}
Label1.Text = sel;
}
}
Try looking at this post answer by NC01.尝试查看 NC01 的此帖子答案。 You may be able to achieve what you want by doing something similar.您可以通过做类似的事情来实现您想要的。
Basically they store the added fields in a hidden field so they are accessible server side.基本上,它们将添加的字段存储在隐藏字段中,因此它们可以在服务器端访问。
I believe that your problem is that when you add an item to a DropDownList client-side (using JavaScript) that when a PostBack occurs, the item will disappear.我相信您的问题是,当您将项目添加到 DropDownList 客户端(使用 JavaScript)时,当 PostBack 发生时,该项目将消失。 The only way around this is to add a hidden server-control to the page and when adding the item client-side, add it to the hidden server-control also, so that when a PostBack occurs you can also add it server-side.解决这个问题的唯一方法是在页面中添加一个隐藏的服务器控件,当在客户端添加项目时,也将它添加到隐藏的服务器控件中,这样当 PostBack 发生时,您也可以在服务器端添加它。
Here's an example.这是一个例子。
https://forums.asp.net/t/1287204.aspx?How+to+add+the+values+to+listbox+control+using+javascript https://forums.asp.net/t/1287204.aspx?How+to+add+the+values+to+listbox+control+using+javascript
aspx file:
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem value="1">Item 1</asp:listitem>
<asp:listitem value="2">Item 2</asp:listitem>
<asp:listitem value="3">Item 3</asp:listitem>
<asp:listitem value="4">Item 4</asp:listitem>
<asp:listitem value="5">Item 5</asp:listitem>
<asp:listitem value="6">Item 6</asp:listitem>
</asp:DropDownList>
<input id="DropDownList1NewItems" type="hidden" name="DropDownList1NewItems" runat="server">
<input type="button" onclick="addItem();" value="Add Item">
</form>
<script type="text/javascript">
<!--
function addItem()
{
// Add the item here...
var ddlRef = document.getElementById('<%= DropDownList1.ClientID %>');
var newOption = window.document.createElement('OPTION');
newOption.text = 'SomeNewItem';
newOption.value = 'SomeNewItem';
ddlRef.options.add(newOption);
var hiddenElementRef = document.getElementById('<%= DropDownList1NewItems.ClientID %>');
if ( hiddenElementRef.value.length > 0 )
hiddenElementRef.value += ';';
hiddenElementRef.value += 'SomeNewItem';
}
// -->
</script>
aspx.cs file:
private void Page_Load(object sender, System.EventArgs e)
{
if ( this.IsPostBack )
{
if ( DropDownList1NewItems.Value.Length > 0 )
{
string [] newItemsArray = DropDownList1NewItems.Value.Split(';');
for (int i=0; i<newItemsArray.Length; i++)
{
string newItem = newItemsArray[i];
DropDownList1.Items.Add(new ListItem(newItem, newItem));
}
DropDownList1NewItems.Value = string.Empty;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.