Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保存动态创建的ListBox ASP.NET C的状态_C#_Asp.net_Html_Listbox - Fatal编程技术网

C# 如何保存动态创建的ListBox ASP.NET C的状态

C# 如何保存动态创建的ListBox ASP.NET C的状态,c#,asp.net,html,listbox,C#,Asp.net,Html,Listbox,我希望在我的表单中有一个ListBoxmulti-selectable,它是从一个文件动态生成的,在提交之后,我希望仍然选择相同的字段 我现在就是这样做的: <select name="select" size="20" multiple style="Width: 100%"> <% Response.WriteFile(@"~\Catalogs\"+usergroup+".htm"); %> </select> 但这样一来,提交后选择就消失了 有没有

我希望在我的表单中有一个ListBoxmulti-selectable,它是从一个文件动态生成的,在提交之后,我希望仍然选择相同的字段

我现在就是这样做的:

<select name="select" size="20" multiple style="Width: 100%">
  <% Response.WriteFile(@"~\Catalogs\"+usergroup+".htm"); %>
</select>
但这样一来,提交后选择就消失了

有没有人能更好地解决我的问题。 该文件不一定是最终的HTML文件,我只是希望能够从该文件中更改列表框的值+文本


非常感谢

您可以在代码隐藏中动态生成项目,并将其添加到列表中。为此,需要将runat=server属性添加到select,或使用asp:DropDownList服务器控件

看起来您正在将文件的内容逐字插入列表中。您可能需要将其转换为更结构化的数据,或者至少将其作为结构化数据读取

例如:

<%-- in markup of control/page --%>
<asp:DropDownList  runat="server" id="ddlCatalogs" />

// in codebehind

// to make this work, you'll need to read the file one line/item as a time
foreach( var item in listOfItemsInFile ){
    var li = new ListItem();

    // TODO: populate "li" from the item's data

    // add it to the server control's collection...now it's a part of the ASP.Net
    // page lifecycle
    this.ddlCatalogs.Controls.Add( option );
}
将下拉列表视为服务器控件后,可以通过多种方式维护状态。默认情况下,ViewState会处理下拉列表,但列表中有许多项,ViewState将变得相当大

或者,您可以转到另一个方向,不涉及服务器控件,只查看原始Request.Form集合,查看它是否包含选择列表的数据,然后将其写回页面

即使在这种情况下,也可能需要以更结构化的方式枚举输入文件,以便选择正确的选项。

谢谢!我现在使用,并在代码隐藏中生成文本和值。只是不知道从这里开始。。