请求,我有小问题。有2页。在One Page through Button1中将文本和值(“123456”,“Jan Novak”)添加到Listbox。我需要这些值来自另一个页面传输中的列表框。当我只有1个值时,它没有问题,但在列表框中并排“双”后有2个值。
有我的代码。
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Session"] != null)
{
ListItemCollection hodnotyState = (ListItemCollection)Session["Session"];
foreach (ListItem i in hodnotyState)
{
ListBox1.Items.Add(i.Text + "|" + i.Value);
}
Session.Clear();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem("123456","Jan Novak"));
}
protected void Button3_Click(object sender, EventArgs e)
{
ListItemCollection kolekce = new ListItemCollection();
foreach (ListItem i in ListBox1.Items)
{
kolekce.Add(i.Text + "|" + i.Value);
Session["Session"] = kolekce;
}
Response.Redirect("page2.aspx");
}
}
page2.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
public partial class page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Session"] != null)
{
ListItemCollection hodnotyState = (ListItemCollection)Session["Session"];
foreach (ListItem i in hodnotyState)
{
ListBox1.Items.Add(i.Text + "|" + i.Value);
}
Session.Clear();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem("987654","John Smith"));
}
protected void Button3_Click(object sender, EventArgs e)
{
ListItemCollection kolekce = new ListItemCollection();
Session.Clear();
foreach (ListItem i in ListBox1.Items)
{
kolekce.Add(i.Text + "|" + i.Value);
Session["Session"] = kolekce;
}
Response.Redirect("Default.aspx");
}
}
否则这些Text + Values不在Listbox中,而是“abreast”。 有更好的经验吗? 谢谢大家。
答案 0 :(得分:3)
问题在于,您将文本和值粘贴到此行中的单个字符串中:kolekce.Add(i.Text + i.Value)
。
您有两种选择:
1)使用不同的集合来存储KeyValuePair,这样就可以保持“密钥”和“值”分开
2)将值一起添加到单个字符串中(正如您现在所做的那样),但使用分隔符(kolekce.Add(i.Text + "|" + i.Value)
)。当您回读该集合时,拆分该分隔符以获取单独的键和值。
在这两种情况下,不要向ListBox1添加单个字符串,而是添加具有单独键和值的新ListItem
。
示例:
如果您存储如下列表:
StringCollection kolekce = new StringCollection();
foreach (ListItem i in ListBox1.Items)
{
kolekce.Add(i.Text + "|" + i.Value);
}
Session["Session"] = kolekce;
(注意:在 foreach-loop之后存储在会话中,一次就足够了)
然后你需要这个来回读:
if (Session["Session"] != null)
{
StringCollection hodnotyState = (StringCollection)Session["Session"];
foreach (string s in hodnotyState)
{
string[] sa = s.Split('|');
ListBox1.Items.Add(new ListItem(sa[0], sa[1]);
}
Session.Remove("Session");
}
答案 1 :(得分:1)
为什么不按照你想要的方式添加它们,而不是在事后做到呢?
Default.aspx函数:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Session"] != null)
{
ListItemCollection hodnotyState = (ListItemCollection)Session["Session"];
foreach (ListItem i in hodnotyState)
{
ListBox1.Items.Add(i);
}
Session.Clear();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ListItem newItem = new ListItem("123456", "Jan Novak");
ListBox1.Items.Add(new ListItem(newItem.Text + newItem.Value, newItem.Value));
}
protected void Button3_Click(object sender, EventArgs e)
{
ListItemCollection kolekce = new ListItemCollection();
foreach (ListItem i in ListBox1.Items)
{
kolekce.Add(i);
}
Session["Session"] = kolekce;
Response.Redirect("page2.aspx");
}
}
并且page2.aspx功能:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Session"] != null)
{
ListItemCollection hodnotyState = (ListItemCollection)Session["Session"];
foreach (ListItem i in hodnotyState)
{
ListBox1.Items.Add(i);
}
Session.Clear();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ListItem newItem = new ListItem("987654", "John Smith");
ListBox1.Items.Add(new ListItem(newItem.Text+newItem.Value, newItem.Value));
}
protected void Button3_Click(object sender, EventArgs e)
{
ListItemCollection kolekce = new ListItemCollection();
Session.Clear();
foreach (ListItem i in ListBox1.Items)
{
kolekce.Add(i);
}
Session["Session"] = kolekce;
Response.Redirect("Default.aspx");
}
}