此处遇到通用列表问题。我正在尝试创建一个网站,您可以使用复选框列表选择项目,并接收您选择的内容。我和完成一样好,但我无法从“ImageSrc”字段中获得正确的值。当我在另一侧打印时,我在两个字段中都得到“名称”值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class filmerHovedside : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
movies();
}
if (Session["Movies"] == null)
{
btnDeleteSession.Visible = false;
}
else
{
btnDeleteSession.Visible = true;
}
}
protected void movies() {
List<Movie> movieList= new List<Movie>();
movieList.Add(new Movie() { Name = "Avengers", ImageSrc= "avengers.jpg" });
movieList.Add(new Movie() { Name = "Pacific Rim", ImageSrc= "pacificRim.jpg" });
movieList.Add(new Movie() { Name = "Star Trek", ImageSrc= "starTrek.jpg" });
movieList.Add(new Movie() { Name = "Superman", ImageSrc= "superman.jpg" });
movieList.Add(new Movie() { Name = "World War Z", ImageSrc= "worldwarz.jpg" });
foreach (Movie movies in movieList)
{
CheckBoxList1.Items.Add(movies.Name);
}
}
public List<Movie> movieList
{
get
{
if (Session["Movie"] == null)
{
List<Movie> movieList= new List<Movie>();
Session.Add("Movies", movieList);
}
return Session["Movies"] as List<Movie>;
}
set
{
Session["Movies"] = value;
}
}
protected void btnSelectMovie_Click(object sender, EventArgs e)
{
if (CheckBoxList1.Items.Cast<ListItem>().Any(item => item.Selected))
{
Session["Movies"] = null;
List<Movie> movies= movieList;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
Movie newMovie = new Movie() { Name = item.Value, ImageSrc = item.Value };
movies.Add(newMovies);
}
}
Response.Redirect("movieCeck.aspx");
}
else
{
StatusLiteral.Text = "Choose at least one movie";
}
}
protected void btnDeleteSession_Click(object sender, EventArgs e)
{
Session["Movie"] = null;
Response.Redirect("~/movieIndex.aspx");
}
}
答案 0 :(得分:0)
看起来你要将Name和ImageSrc都设置为item.Value。
答案 1 :(得分:0)
可能是因为错误的地方
if (Session["Movie"] == null)
if (Session["Movies"] == null)