大家好,
拜托,我有一个小问题。有 2 页。在通过 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");
}
}
否则这些文本+值不在列表框中,而是“并列”。有更好的经验吗?谢谢你们。