简体   繁体   English

使用ASP.net C#从html选择列表中获取所有项目

[英]Get all items from html select list using ASP.net C#

I have the following select list: 我有以下选择列表:

<select name="Context" id="Context" size="5">
<option>Context1</option>
<option>Context2</option>
<option>Context3</option>
<option>Context4</option>
<option>Context5</option>
</select>

I am using javascript to move the items of this select list up and down (this is working). 我正在使用javascript上下移动此选择列表的项(正在运行)。 I would like to feed the resulting order back to ASP.net using C#. 我想使用C#将生成的订单反馈回ASP.net。

If I use the following line of code I get only the selected item, how do I get the whole list? 如果我使用下面的代码行,则仅获得所选项目,如何获得整个列表?

 string items = Request.Form["Context"];

You can get all items like this. 您可以得到所有类似的物品。

var ddlArray= new Array();
var ddl = document.getElementById('Context');
for (i = 0; i < ddl.options.length; i++) {
    ddlArray[i] = ddl .options[i].value;
}

Now pass this JavaScript array to your code behind. 现在,将此JavaScript数组传递给后面的代码。

One answer is to modify the select to make it a ListBox and then access it programattically on the server: 一个答案是修改select以使其成为一个ListBox ,然后在服务器上以编程方式对其进行访问:

<asp:ListBox runat="server" name="lstContext" id="lstContext" size="5">
    <asp:ListItem>Context1</asp:ListItem>
    <asp:ListItem>Context2</asp:ListItem>
    <asp:ListItem>Context3</asp:ListItem>
    <asp:ListItem>Context4</asp:ListItem>
    <asp:ListItem>Context5</asp:ListItem>
</asp:ListBox>

Note that I renamed it lstContext as calling it Context will cause build failures (due to sharing a name with the existing Context object). 请注意,我将其重命名为lstContext因为将其称为Context会导致构建失败(由于与现有Context对象共享名称)。

Then in your code, to access the values in the order they appear: 然后在您的代码中,以它们出现的顺序访问值:

for (int i = 0; i < lstContext.Items.Count; i++)
{
    string item = lstContext.Items[i].Value;
    // Do something with it
}

Your javascript should still work on the ListBox as it does for the select , so this should remain unaffected. 您的javascript仍应像对select一样在ListBox上工作,因此应保持不受影响。

create a hidden field (runat=server) and store the shuffled <options> into the hidden field; 创建一个隐藏字段(runat = server)并将改组后的<options>存储到该隐藏字段中; You can access content of hidden field at server side by string itemsOrderedChanged = Request.Form[<HiddenFieldId>]; 您可以通过string itemsOrderedChanged = Request.Form[<HiddenFieldId>];在服务器端访问隐藏字段的内容string itemsOrderedChanged = Request.Form[<HiddenFieldId>];

try to pass JavaScript value to hidden field like this . 尝试像这样将JavaScript值传递给隐藏字段。

var hidden=document.getElementById('hidValue');
var string strValues="";
for(var i=0i<ddlArray.Length;i++){
 strValues+=ddlArray[i]+",";
}

hidden.value=strValues;

And in page_load or what_ever event get this string and split it. 并在page_load或what_ever事件中获取此字符串并将其拆分。

protected HtmlControls.HtmlInputHidden hidValue;

protected void Page_Load(object sender, System.EventArgs e)
{
   dynamic hiddenValue = hidValue.Value;
   string [] arr=hiddenValue.Split(",");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在RadioButtonList中选择一个项目,如何从asp.net中的数据库获取到ListBox的项目列表c# - Selecting an item in the RadioButtonList, how to get a list of items to a ListBox from database in asp.net c# 在C#asp.net中的TFS中获取所有项目列表? - Get all the project list in TFS in C# asp.net? 从表中检索所有项目,其中表的文本列在项目列表中(ASP.NET Core 2.2 C#LINQ) - Retrieve all the items from a table where the text of a table column is contained in a list of items (ASP.NET Core 2.2 C# LINQ) 有没有办法使用带有C#作为后端的ASP.NET获取域中所有用户的列表? - Is there a way to get a list of all the users on a domain using ASP.NET with C# as the backend? 如何选择列表框中的所有项目并在ASP.NET C#Webform中将它们串联起来? - How to select all items in a Listbox and concatenate them in ASP.NET C# Webform? C#ASP.NET Web Api控制器 - 使用SqlCommand从表中获取所有行 - C# ASP.NET Web Api Controller - Get all rows from a table using SqlCommand 拖放列表项后获得列表顺序asp.net C# - Get list order after dragging and dropping list items asp.net c# 如何使用 C# ASP.Net 从 XML 文档中获取特定 XML 元素的列表? - How to get list of specific XML elements from XML document using C# ASP.Net? 如何在不满足 C#、ASP.NET CORE 中的特定条件的情况下显示列表中的所有项目? - How to display all items in a list without satisfying a particular condition in C#,ASP.NET CORE? 如何将项目添加到列表中<T>从控制器中的表单输入而不使用视图? ASP.NET MVC C# - How can I add items to a list<T> from form input in a controller without using views? ASP.NET MVC C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM